| 1 |
// Copyright (c) 2017, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
//go:build js |
| 5 |
|
| 6 |
package interp |
| 7 |
|
| 8 |
import ( |
| 9 |
"io" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
// js/wasm has no OS pipes and no subprocesses, so pipes are in-process |
| 14 |
// [io.Pipe]s and any reader can be used as stdin directly. Read |
| 15 |
// deadlines are not supported: SetReadDeadline is a no-op, so a |
| 16 |
// blocked `read` builtin unblocks on the next write rather than on |
| 17 |
// context cancellation. |
| 18 |
|
| 19 |
// stdinFile is the runner's standard input; elsewhere it is an |
| 20 |
// [*os.File], but on js/wasm any reader satisfying this interface works. |
| 21 |
type stdinFile interface { |
| 22 |
io.ReadCloser |
| 23 |
SetReadDeadline(t time.Time) error |
| 24 |
} |
| 25 |
|
| 26 |
// jsReader adapts a plain reader into a stdinFile. |
| 27 |
type jsReader struct { |
| 28 |
io.Reader |
| 29 |
} |
| 30 |
|
| 31 |
func (r jsReader) Close() error { |
| 32 |
if c, ok := r.Reader.(io.Closer); ok { |
| 33 |
return c.Close() |
| 34 |
} |
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
func (jsReader) SetReadDeadline(t time.Time) error { return nil } |
| 39 |
|
| 40 |
// newPipe returns an in-process pipe. |
| 41 |
func newPipe() (stdinFile, *io.PipeWriter, error) { |
| 42 |
pr, pw := io.Pipe() |
| 43 |
return jsReader{pr}, pw, nil |
| 44 |
} |
| 45 |
|
| 46 |
// newStdinFile converts a reader into the runner's stdin. Without |
| 47 |
// subprocesses there is no need to copy through a pipe. |
| 48 |
func newStdinFile(r io.Reader) (stdinFile, error) { |
| 49 |
if r == nil { |
| 50 |
return nil, nil |
| 51 |
} |
| 52 |
return jsReader{r}, nil |
| 53 |
} |
| 54 |
|
| 55 |
// stdinTerminal always reports false, as js/wasm has no terminals. |
| 56 |
func stdinTerminal(stdin stdinFile) (int, bool) { return -1, false } |