wasi-libc/libc-top-half/headers/private/wasi/libc-environ-compat.h
Dan Gohman 9efc2f4283
Lazy-initialize the environment variables. (#184)
* Lazy-initialize the environment variables.

This is the first in a series of PRs to make it easier to use WASI libc
in Wasm modules that don't have a `main` function. By initializing the
environment on demand, we avoid depending on having `__wasm_call_ctors`
run.

This uses weak symbols strategically to ensure that if `environ` is
used, it is initialized eagerly, but if only `getenv` and friends
are used, the environment is initialized lazily.

Eventually, I expect we'll have a convention for wasm modules without
main functions which will allow the `__wasm_call_ctors` function to be
called automatically, but this helps in simple cases for now.

Fixes #180.

* Add comments explaining the libc-environ-compat.h header usage.
2020-03-19 09:32:41 -07:00

13 lines
442 B
C

// This header file is meant to be included withinin the body of a function
// which uses `__environ`. Code using `__environ` expects it will be initialized
// eagerly. `__wasilibc_environ` is initialized lazily. Provide `__environ` as
// an alias and arrange for the lazy initialization to be performed.
extern char **__wasilibc_environ;
__wasilibc_ensure_environ();
#ifndef __wasilibc_environ
#define __environ __wasilibc_environ
#endif