tinygo/src/runtime/nonhosted.go
Ayke van Laethem fb33f3813d runtime: only initialize os.runtime_args when needed
This generally means that code size is reduced, especially when the os
package is not imported.

Specifically:

  - On Linux (which currently statically links musl), it avoids calling
    malloc, which avoids including the musl C heap for small programs
    saving around 1.6kB.
  - On WASI, it avoids initializing the args slice when the os package
    is not used. This reduces binary size by around 1kB.
2021-11-05 08:50:36 +01:00

56 строки
1,2 КиБ
Go

// +build baremetal js
package runtime
// This file is for non-hosted environments, that don't support command line
// parameters or environment variables. To still be able to run certain tests,
// command line parameters and environment variables can be passed to the binary
// by setting the variables `runtime.osArgs` and `runtime.osEnv`, both of which
// are strings separated by newlines.
//
// The primary use case is `tinygo test`, which takes some parameters (such as
// -test.v).
// This is the default set of arguments, if nothing else has been set.
var args = []string{"/proc/self/exe"}
//go:linkname os_runtime_args os.runtime_args
func os_runtime_args() []string {
return args
}
var env []string
//go:linkname syscall_runtime_envs syscall.runtime_envs
func syscall_runtime_envs() []string {
return env
}
var osArgs string
var osEnv string
func init() {
if osArgs != "" {
s := osArgs
start := 0
for i := 0; i < len(s); i++ {
if s[i] == 0 {
args = append(args, s[start:i])
start = i + 1
}
}
args = append(args, s[start:])
}
if osEnv != "" {
s := osEnv
start := 0
for i := 0; i < len(s); i++ {
if s[i] == 0 {
env = append(env, s[start:i])
start = i + 1
}
}
env = append(env, s[start:])
}
}