Arguments and environment variables
Cheerp (nightly) has support for passing arguments and environment variables to your programs. The way you pass them depends on whether you’re compiling for WASI, ES6 or something else.
For all the examples we’ll use the following example program:
WASI
Cheerp uses the WASI-CLI interface to get the arguments and environment variables, meaning that it will work on any runtime that has implemented the WASI-CLI proposal.
Here we’ll be using wasmtime
:
ES6 modules
For ES6 modules, you can pass the arguments through the initialization function like this:
Other
When not building WASI or ES6 modules, you can provide environment variables and program arguments by defining the following global variables in JavaScript before
the call to main
:
CHEERP_ENV
CHEERP_ARGV
Do note that changes to CHEERP_ENV
after main
has run will not be reflected in environ
.
Then we open the file and place the following near the top:
Then running it:
NodeJS
When running your program with NodeJS, you can also specify program arguments and/or environment variables like this:
This will only work if you didn’t pass them in a different way (e.g. using CHEERP_ENV
or CHEERP_ARGV
)
Limitations
Due to technical reasons, it is currently not possible to receive arguments and/or environment variables in main
when
the main
function is in a different section than the target. For example the following won’t work when compiled with
the flag: -target cheerp
:
In these cases, you can still get environment variables with the getenv
function, the only limitation is that you
can’t get them for the arguments to main
.