wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

C#: Tidy client facing function location

Open yowl opened this issue 7 months ago • 1 comments

cc @bytecodealliance/dotnet-core As part of the future work, I'm was trying to tidy up the location of the import and export methods. At present we have this for the import-export-same-func.wit codegen test

// Generated by `wit-bindgen` 0.43.0. DO NOT EDIT!
// <auto-generated />
#nullable enable

namespace MyWorldWorld {

    public interface IMyWorldWorld {

        static abstract void Purple();

    }

    namespace exports {
        public static class MyWorldWorld
        {

            internal static class PurpleWasmInterop
            {
                [global::System.Runtime.InteropServices.DllImportAttribute("$root", EntryPoint = "purple"), global::System.Runtime.InteropServices.WasmImportLinkageAttribute]
                internal static extern void wasmImportPurple();
            }

            public static unsafe void Purple()
            {
                PurpleWasmInterop.wasmImportPurple();

            }

            [global::System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(EntryPoint = "purple")]
            public static unsafe void wasmExportPurple() {

                MyWorldWorldImpl.Purple();

            }
        }
    }

}

As you can see the user facting imported function is in the exports namespace, but the imported function is in the interface IMyWorldWorld. This has the advantage that they can have the same name without a conflict but is not very symmetrical or intuitive. I propose to change this to

// Generated by `wit-bindgen` 0.44.0. DO NOT EDIT!
// <auto-generated />
#nullable enable

namespace MyWorldWorld {

    public interface IMyWorldWorld {

        public interface Exports
        {
            public static unsafe void Purple()
            {
                exports.MyWorldWorldInterop.PurpleWasmInterop.wasmImportPurple();

            }

        }

        public interface Imports
        {
            static abstract void Purple();

        }

    }

    namespace exports {
        public static class MyWorldWorldInterop
        {

            internal static class PurpleWasmInterop
            {
                [global::System.Runtime.InteropServices.DllImportAttribute("$root", EntryPoint = "purple"), global::System.Runtime.InteropServices.WasmImportLinkageAttribute]
                internal static extern void wasmImportPurple();
            }

            [global::System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(EntryPoint = "purple")]
            public static unsafe void wasmExportPurple() {

                MyWorldWorldImpl.Purple();

            }
        }
    }

}

Where both the import and export are in the interface, but separated to avoid name conflicts by nested interfaces, Imports and Exports.

Does this sound bad to anyone?

yowl avatar Aug 21 '25 01:08 yowl

I have noticed this as well and I think the proposed solution would be an improvement

jsturtevant avatar Aug 22 '25 21:08 jsturtevant