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

     1  //go:build js
     2  
     3  package syscall
     4  
     5  import (
     6  	"syscall/js"
     7  )
     8  
     9  // fsCall emulates a file system-related syscall via a corresponding NodeJS fs
    10  // API.
    11  //
    12  // This version is similar to the upstream, but it gracefully handles missing fs
    13  // methods (allowing for smaller prelude) and removes a workaround for an
    14  // obsolete NodeJS version.
    15  func fsCall(name string, args ...interface{}) (js.Value, error) {
    16  	type callResult struct {
    17  		val js.Value
    18  		err error
    19  	}
    20  
    21  	c := make(chan callResult, 1)
    22  	f := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
    23  		var res callResult
    24  
    25  		// Check that args has at least one element, then check both IsUndefined() and IsNull() on
    26  		// that element. In some situations, BrowserFS calls the callback without arguments or with
    27  		// an undefined argument: https://github.com/gopherjs/gopherjs/pull/1118
    28  		if len(args) >= 1 {
    29  			if jsErr := args[0]; !jsErr.IsUndefined() && !jsErr.IsNull() {
    30  				res.err = mapJSError(jsErr)
    31  			}
    32  		}
    33  
    34  		res.val = js.Undefined()
    35  		if len(args) >= 2 {
    36  			res.val = args[1]
    37  		}
    38  
    39  		c <- res
    40  		return nil
    41  	})
    42  	defer f.Release()
    43  	if jsFS.Get(name).IsUndefined() {
    44  		return js.Undefined(), ENOSYS
    45  	}
    46  	jsFS.Call(name, append(args, f)...)
    47  	res := <-c
    48  	return res.val, res.err
    49  }