Browse Source

Revert #131, renaming `main` back to `run`. (#165)

Changing LLVM and/or Rust to avoid special handling of `main` is a fair
amount of work, and there could be other toolchains with similar special
rules for functions named `main`, so rename the command entrypoint back
to `run`.

We could potentially re-evaluate this in the future, such as in a
preview3 timeframe, but for now, let's go with the simplest thing that
works.
pull/6374/head
Dan Gohman 2 years ago
committed by GitHub
parent
commit
4f84e64117
  1. 6
      build.rs
  2. 17
      src/lib.rs
  3. 27
      src/main.s

6
build.rs

@ -14,12 +14,6 @@ fn main() {
out_dir.to_str().unwrap()
);
// LLVM has special handling for `main` so use a .s file to define the main
// function for the adapter. See the comments in src/main.s for details.
if env::var("CARGO_FEATURE_COMMAND").is_ok() {
println!("cargo:rustc-link-arg=src/main.o");
}
// Some specific flags to `wasm-ld` to inform the shape of this adapter.
// Notably we're importing memory from the main module and additionally our
// own module has no stack at all since it's specifically allocated at

17
src/lib.rs

@ -31,7 +31,7 @@ pub mod bindings {
raw_strings,
// The generated definition of command will pull in std, so we are defining it
// manually below instead
skip: ["main", "get-directories", "get-environment"],
skip: ["run", "get-directories", "get-environment"],
});
#[cfg(feature = "reactor")]
@ -43,6 +43,17 @@ pub mod bindings {
});
}
#[no_mangle]
#[cfg(feature = "command")]
pub unsafe extern "C" fn run() -> u32 {
#[link(wasm_import_module = "__main_module__")]
extern "C" {
fn _start();
}
_start();
0
}
// The unwrap/expect methods in std pull panic when they fail, which pulls
// in unwinding machinery that we can't use in the adapter. Instead, use this
// extension trait to get postfixed upwrap on Option and Result.
@ -197,7 +208,7 @@ impl ImportAlloc {
}
}
/// This allocator is only used for the `main` entrypoint.
/// This allocator is only used for the `run` entrypoint.
///
/// The implementation here is a bump allocator into `State::long_lived_arena` which
/// traps when it runs out of data. This means that the total size of
@ -2092,7 +2103,7 @@ struct State {
/// Long-lived bump allocated memory arena.
///
/// This is used for the cabi_export_realloc to allocate data passed to the
/// `main` entrypoint. Allocations in this arena are safe to use for
/// `run` entrypoint. Allocations in this arena are safe to use for
/// the lifetime of the State struct. It may also be used for import allocations
/// which need to be long-lived, by using `import_alloc.with_arena`.
long_lived_arena: BumpArena,

27
src/main.s

@ -1,27 +0,0 @@
# The component model CLI world exports its entrypoint under the name
# "main". However, LLVM has special handling for functions named `main`
# in order to handle the `main(void)` vs `main(int argc, char **argv)`
# difference on Wasm where the caller needs to know the exact signature.
# To avoid this, define a function with a different name and export it
# as `main`.
#
# To generate the `main.o` file from this `main.s` file, compile with
# `clang --target=wasm32-wasi -c main.s`
.text
.functype main () -> (i32)
.export_name main, main
.functype _start () -> ()
.import_name _start, _start
.import_module _start, __main_module__
.section .text.main,"",@
.hidden main
.globl main
.type main,@function
main:
.functype main () -> (i32)
call _start
i32.const 0
return
end_function
.no_dead_strip main
Loading…
Cancel
Save