mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-08 13:03:39 +00:00
runtime: implement command line arguments in hosted environments
Implement command line arguments for Linux, MacOS and WASI.
This commit is contained in:
committed by
Ron Evans
parent
c47cdfa66f
commit
7b761fac78
@@ -43,9 +43,34 @@ func postinit() {}
|
||||
|
||||
// Entry point for Go. Initialize all packages and call main.main().
|
||||
//export main
|
||||
func main() int {
|
||||
func main(argc int32, argv *unsafe.Pointer) int {
|
||||
preinit()
|
||||
|
||||
// Make args global big enough so that it can store all command line
|
||||
// arguments. Unfortunately this has to be done with some magic as the heap
|
||||
// is not yet initialized.
|
||||
argsSlice := (*struct {
|
||||
ptr unsafe.Pointer
|
||||
len uintptr
|
||||
cap uintptr
|
||||
})(unsafe.Pointer(&args))
|
||||
argsSlice.ptr = malloc(uintptr(argc) * (unsafe.Sizeof(uintptr(0))) * 3)
|
||||
argsSlice.len = 0
|
||||
argsSlice.cap = uintptr(argc)
|
||||
|
||||
// Initialize command line parameters.
|
||||
for *argv != nil {
|
||||
// Convert the C string to a Go string.
|
||||
length := strlen(*argv)
|
||||
argString := _string{
|
||||
length: length,
|
||||
ptr: (*byte)(*argv),
|
||||
}
|
||||
args = append(args, *(*string)(unsafe.Pointer(&argString)))
|
||||
// This is the Go equivalent of "argc++" in C.
|
||||
argv = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + unsafe.Sizeof(argv)))
|
||||
}
|
||||
|
||||
// Obtain the initial stack pointer right before calling the run() function.
|
||||
// The run function has been moved to a separate (non-inlined) function so
|
||||
// that the correct stack pointer is read.
|
||||
@@ -65,9 +90,6 @@ func runMain() {
|
||||
//go:extern environ
|
||||
var environ *unsafe.Pointer
|
||||
|
||||
//export strlen
|
||||
func strlen(ptr unsafe.Pointer) uintptr
|
||||
|
||||
//go:linkname syscall_runtime_envs syscall.runtime_envs
|
||||
func syscall_runtime_envs() []string {
|
||||
// Count how many environment variables there are.
|
||||
|
||||
Reference in New Issue
Block a user