github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/nonhosted.go (about)

     1  //go:build baremetal || js
     2  
     3  package runtime
     4  
     5  // This file is for non-hosted environments, that don't support command line
     6  // parameters or environment variables. To still be able to run certain tests,
     7  // command line parameters and environment variables can be passed to the binary
     8  // by setting the variables `runtime.osArgs` and `runtime.osEnv`, both of which
     9  // are strings separated by newlines.
    10  //
    11  // The primary use case is `tinygo test`, which takes some parameters (such as
    12  // -test.v).
    13  
    14  // This is the default set of arguments, if nothing else has been set.
    15  var args = []string{"/proc/self/exe"}
    16  
    17  //go:linkname os_runtime_args os.runtime_args
    18  func os_runtime_args() []string {
    19  	return args
    20  }
    21  
    22  var env []string
    23  
    24  //go:linkname syscall_runtime_envs syscall.runtime_envs
    25  func syscall_runtime_envs() []string {
    26  	return env
    27  }
    28  
    29  var osArgs string
    30  var osEnv string
    31  
    32  func init() {
    33  	if osArgs != "" {
    34  		s := osArgs
    35  		start := 0
    36  		for i := 0; i < len(s); i++ {
    37  			if s[i] == 0 {
    38  				args = append(args, s[start:i])
    39  				start = i + 1
    40  			}
    41  		}
    42  		args = append(args, s[start:])
    43  	}
    44  
    45  	if osEnv != "" {
    46  		s := osEnv
    47  		start := 0
    48  		for i := 0; i < len(s); i++ {
    49  			if s[i] == 0 {
    50  				env = append(env, s[start:i])
    51  				start = i + 1
    52  			}
    53  		}
    54  		env = append(env, s[start:])
    55  	}
    56  }