@@ -53,14 +53,38 @@ get-numbered-pipe fd/int:
5353 else:
5454 return OpenPipe .from-std_ ( fd-to-pipe_ pipe-resource-group_ fd )
5555
56- class OpenPipe extends Object with io.InMixin io.OutMixin implements old-reader.Reader :
56+ class OpenPipeReader_ extends io .CloseableReader :
57+ pipe_ / OpenPipe
58+
59+ constructor .pipe_ :
60+
61+ read_ -> ByteArray? :
62+ return pipe_ .read_
63+
64+ close_ -> none :
65+ pipe_ .close
66+
67+ class OpenPipeWriter extends io .CloseableWriter :
68+ pipe_ / OpenPipe
69+
70+ constructor .pipe_ :
71+
72+ try-write_ data / io.Data from / int to / int -> int :
73+ return pipe_ .try-write_ data from to
74+
75+ close_ -> none :
76+ pipe_ .close
77+
78+ class OpenPipe implements old-reader.Reader :
5779 resource_ := ?
5880 state_ := ?
5981 pid := null
6082 child-process-name_ / string?
6183 input_ / int := UNKNOWN-DIRECTION_
6284
6385 fd := -1 // Other end of descriptor, for child process.
86+ in_ / OpenPipeReader_? := null
87+ out_ / OpenPipeWriter? := null
6488
6589 constructor input / bool --child-process-name = "child process" :
6690 group := pipe-resource-group_
@@ -70,11 +94,29 @@ class OpenPipe extends Object with io.InMixin io.OutMixin implements old-reader.
7094 resource_ = pipe-pair [ 0 ]
7195 fd = pipe-pair [ 1 ]
7296 state_ = monitor .ResourceState_ pipe-resource-group_ resource_
97+ if input :
98+ in_ = null
99+ out_ = OpenPipeWriter this
100+ else:
101+ in_ = OpenPipeReader_ this
102+ out_ = null
73103
74104 constructor .from-std_ .resource_ :
75105 group := pipe-resource-group_
76106 child-process-name_ = null
77107 state_ = monitor .ResourceState_ pipe-resource-group_ resource_
108+ in_ = OpenPipeReader_ this
109+ out_ = OpenPipeWriter this
110+
111+ in -> io.CloseableReader :
112+ if not in_ :
113+ throw "use of output pipe as input"
114+ return in_
115+
116+ out -> io.CloseableWriter :
117+ if not out_ :
118+ throw "use of input pipe as output"
119+ return out_
78120
79121 /**
80122 Deprecated. Use 'read' on $in instead.
@@ -83,8 +125,6 @@ class OpenPipe extends Object with io.InMixin io.OutMixin implements old-reader.
83125 return in .read
84126
85127 read_ -> ByteArray? :
86- if input_ == PARENT-TO-CHILD_ :
87- throw "read from an output pipe"
88128 while true :
89129 state_ .wait-for-state READ-EVENT_ | CLOSE-EVENT_
90130 result := read-from-pipe_ resource_
@@ -104,16 +144,14 @@ class OpenPipe extends Object with io.InMixin io.OutMixin implements old-reader.
104144 return try-write_ x from to
105145
106146 try-write_ data / io.Data from / int to / int -> int :
107- if input_ == CHILD-TO-PARENT_ :
108- throw "write to an input pipe"
109147 if from == to : return 0
110148 state_ .wait-for-state WRITE-EVENT_ | ERROR-EVENT_
111149 bytes-written := write-to-pipe_ resource_ data from to
112150 if bytes-written == 0 : state_ .clear-state WRITE-EVENT_
113151 return bytes-written
114152
115153 close :
116- close_ resource_
154+ close-resource_ resource_
117155 if state_ :
118156 state_ .dispose
119157 state_ = null
@@ -158,10 +196,10 @@ write-to-pipe_ pipe data/io.Data from to:
158196read-from-pipe_ pipe :
159197 #primitive .pipe .read
160198
161- close_ pipe :
162- return close_ pipe pipe-resource-group_
199+ close-resource_ pipe :
200+ return close-resource_ pipe pipe-resource-group_
163201
164- close_ pipe resource-group :
202+ close-resource_ pipe resource-group :
165203 #primitive .pipe .close
166204
167205/// Use the stdin/stdout/stderr that the parent Toit process has.
0 commit comments