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

Method generate_guest_export

crates/rust/src/interface.rs:1097–1218  ·  view source on GitHub ↗
(
        &mut self,
        func: &Function,
        interface: Option<&WorldKey>,
        trait_name: &str,
        async_: bool,
    )

Source from the content-addressed store, hash-verified

1095 }
1096
1097 fn generate_guest_export(
1098 &mut self,
1099 func: &Function,
1100 interface: Option<&WorldKey>,
1101 trait_name: &str,
1102 async_: bool,
1103 ) {
1104 let name_snake = func.name.to_snake_case().replace('.', "_");
1105
1106 self.generate_payloads("[export]", func, interface);
1107
1108 uwrite!(
1109 self.src,
1110 "\
1111 #[doc(hidden)]
1112 #[allow(non_snake_case, unused_unsafe)]
1113 pub unsafe fn _export_{name_snake}_cabi<T_: {trait_name}>\
1114 ",
1115 );
1116 let params = self.print_export_sig(func, async_);
1117 self.push_str(" { unsafe {");
1118
1119 if !self.r#gen.opts.disable_run_ctors_once_workaround {
1120 let run_ctors_once = self.path_to_run_ctors_once();
1121 // Before executing any other code, use this function to run all
1122 // static constructors, if they have not yet been run. This is a
1123 // hack required to work around wasi-libc ctors calling import
1124 // functions to initialize the environment.
1125 //
1126 // See
1127 // https://github.com/bytecodealliance/preview2-prototyping/issues/99
1128 // for more details.
1129 uwrite!(
1130 self.src,
1131 "#[cfg(target_arch=\"wasm32\")]\n{run_ctors_once}();",
1132 );
1133 }
1134
1135 let mut f = FunctionBindgen::new(self, params, self.wasm_import_module, false, false);
1136 let variant = if async_ {
1137 AbiVariant::GuestExportAsync
1138 } else {
1139 AbiVariant::GuestExport
1140 };
1141 abi::call(
1142 f.r#gen.resolve,
1143 variant,
1144 LiftLower::LiftArgsLowerResults,
1145 func,
1146 &mut f,
1147 async_,
1148 );
1149 let FunctionBindgen {
1150 needs_cleanup_list,
1151 src,
1152 handle_decls,
1153 ..
1154 } = f;

Callers 1

generate_exportsMethod · 0.80

Calls 11

newFunction · 0.85
callFunction · 0.85
post_returnFunction · 0.85
generate_payloadsMethod · 0.80
print_export_sigMethod · 0.80
async_support_pathMethod · 0.80
path_to_vecMethod · 0.80
print_post_return_sigMethod · 0.80
push_strMethod · 0.45

Tested by

no test coverage detected