github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/natives/src/syscall/syscall_js_wasm.go (about)

     1  package syscall
     2  
     3  import (
     4  	"syscall/js"
     5  )
     6  
     7  func runtime_envs() []string {
     8  	process := js.Global().Get("process")
     9  	if process.IsUndefined() {
    10  		return nil
    11  	}
    12  	jsEnv := process.Get("env")
    13  	if jsEnv.IsUndefined() {
    14  		return nil
    15  	}
    16  	envkeys := js.Global().Get("Object").Call("keys", jsEnv)
    17  	envs := make([]string, envkeys.Length())
    18  	for i := 0; i < envkeys.Length(); i++ {
    19  		key := envkeys.Index(i).String()
    20  		envs[i] = key + "=" + jsEnv.Get(key).String()
    21  	}
    22  	return envs
    23  }
    24  
    25  func setenv_c(k, v string) {
    26  	process := js.Global().Get("process")
    27  	if process.IsUndefined() {
    28  		return
    29  	}
    30  	process.Get("env").Set(k, v)
    31  }
    32  
    33  func unsetenv_c(k string) {
    34  	process := js.Global().Get("process")
    35  	if process.IsUndefined() {
    36  		return
    37  	}
    38  	process.Get("env").Delete(k)
    39  }
    40  
    41  func setStat(st *Stat_t, jsSt js.Value) {
    42  	// This method is an almost-exact copy of upstream, except for 4 places where
    43  	// time stamps are obtained as floats in lieu of int64. Upstream wasm emulates
    44  	// a 64-bit architecture and millisecond-based timestamps fit within an int
    45  	// type. GopherJS is 32-bit and use of 32-bit ints causes timestamp truncation.
    46  	// We get timestamps as float64 (which matches JS-native representation) and
    47  	// convert then to int64 manually, since syscall/js.Value doesn't have an
    48  	// Int64 method.
    49  	st.Dev = int64(jsSt.Get("dev").Int())
    50  	st.Ino = uint64(jsSt.Get("ino").Int())
    51  	st.Mode = uint32(jsSt.Get("mode").Int())
    52  	st.Nlink = uint32(jsSt.Get("nlink").Int())
    53  	st.Uid = uint32(jsSt.Get("uid").Int())
    54  	st.Gid = uint32(jsSt.Get("gid").Int())
    55  	st.Rdev = int64(jsSt.Get("rdev").Int())
    56  	st.Size = int64(jsSt.Get("size").Int())
    57  	st.Blksize = int32(jsSt.Get("blksize").Int())
    58  	st.Blocks = int32(jsSt.Get("blocks").Int())
    59  	atime := int64(jsSt.Get("atimeMs").Float()) // Int64
    60  	st.Atime = atime / 1000
    61  	st.AtimeNsec = (atime % 1000) * 1000000
    62  	mtime := int64(jsSt.Get("mtimeMs").Float()) // Int64
    63  	st.Mtime = mtime / 1000
    64  	st.MtimeNsec = (mtime % 1000) * 1000000
    65  	ctime := int64(jsSt.Get("ctimeMs").Float()) // Int64
    66  	st.Ctime = ctime / 1000
    67  	st.CtimeNsec = (ctime % 1000) * 1000000
    68  }
    69  
    70  func Exit(code int) {
    71  	if process := js.Global().Get("process"); !process.IsUndefined() {
    72  		process.Call("exit", code)
    73  		return
    74  	}
    75  	if code != 0 {
    76  		js.Global().Get("console").Call("warn", "Go program exited with non-zero code:", code)
    77  	}
    78  }