Skip to content

Commit 0713cbb

Browse files
committed
Make nqp::{write,print,say}fh on JVM return number of bytes written.
This brings NQP/JVM in line with the Parrot and Moar backends, and fixes three failing tests in t/nqp/19-file-ops.t
1 parent f4514bb commit 0713cbb

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,9 +1901,9 @@ QAST::OperationsJAST.map_classlib_core_op('setencoding', $TYPE_OPS, 'setencoding
19011901
QAST::OperationsJAST.map_classlib_core_op('setinputlinesep', $TYPE_OPS, 'setinputlinesep', [$RT_OBJ, $RT_STR], $RT_OBJ, :tc);
19021902
QAST::OperationsJAST.map_classlib_core_op('tellfh', $TYPE_OPS, 'tellfh', [$RT_OBJ], $RT_INT, :tc);
19031903
QAST::OperationsJAST.map_classlib_core_op('readfh', $TYPE_OPS, 'readfh', [$RT_OBJ, $RT_OBJ, $RT_INT], $RT_OBJ, :tc);
1904-
QAST::OperationsJAST.map_classlib_core_op('writefh', $TYPE_OPS, 'writefh', [$RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
1905-
QAST::OperationsJAST.map_classlib_core_op('printfh', $TYPE_OPS, 'printfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);
1906-
QAST::OperationsJAST.map_classlib_core_op('sayfh', $TYPE_OPS, 'sayfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);QAST::OperationsJAST.map_classlib_core_op('sayfh', $TYPE_OPS, 'sayfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);
1904+
QAST::OperationsJAST.map_classlib_core_op('writefh', $TYPE_OPS, 'writefh', [$RT_OBJ, $RT_OBJ], $RT_INT, :tc);
1905+
QAST::OperationsJAST.map_classlib_core_op('printfh', $TYPE_OPS, 'printfh', [$RT_OBJ, $RT_STR], $RT_INT, :tc);
1906+
QAST::OperationsJAST.map_classlib_core_op('sayfh', $TYPE_OPS, 'sayfh', [$RT_OBJ, $RT_STR], $RT_INT, :tc);QAST::OperationsJAST.map_classlib_core_op('sayfh', $TYPE_OPS, 'sayfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);
19071907
QAST::OperationsJAST.map_classlib_core_op('flushfh', $TYPE_OPS, 'flushfh', [$RT_OBJ], $RT_OBJ, :tc);QAST::OperationsJAST.map_classlib_core_op('sayfh', $TYPE_OPS, 'sayfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);
19081908
QAST::OperationsJAST.map_classlib_core_op('readlinefh', $TYPE_OPS, 'readlinefh', [$RT_OBJ], $RT_STR, :tc);
19091909
QAST::OperationsJAST.map_classlib_core_op('readlineintfh', $TYPE_OPS, 'readlineintfh', [$RT_OBJ, $RT_STR], $RT_STR, :tc);

src/vm/jvm/runtime/org/perl6/nqp/io/IIOSyncWritable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.perl6.nqp.runtime.ThreadContext;
44

55
public interface IIOSyncWritable {
6-
public void print(ThreadContext tc, String s);
7-
public void say(ThreadContext tc, String s);
8-
public void write(ThreadContext tc, byte[] bytes);
6+
public long print(ThreadContext tc, String s);
7+
public long say(ThreadContext tc, String s);
8+
public long write(ThreadContext tc, byte[] bytes);
99
public void flush(ThreadContext tc);
1010
}

src/vm/jvm/runtime/org/perl6/nqp/io/StandardWriteHandle.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,26 @@ public void setEncoding(ThreadContext tc, Charset cs) {
4040
dec = cs.newDecoder();
4141
}
4242

43-
public void write(ThreadContext tc, byte[] bytes) {
43+
public long write(ThreadContext tc, byte[] bytes) {
4444
ps.write(bytes, 0, bytes.length);
45+
return bytes.length;
4546
}
4647

47-
public void print(ThreadContext tc, String s) {
48+
public long print(ThreadContext tc, String s) {
4849
try {
4950
ByteBuffer buffer = enc.encode(CharBuffer.wrap(s));
5051
byte[] bytes = buffer.array();
5152
ps.write(bytes, 0, buffer.limit());
53+
return bytes.length;
5254
} catch (IOException e) {
5355
throw ExceptionHandling.dieInternal(tc, e);
5456
}
5557
}
5658

57-
public void say(ThreadContext tc, String s) {
58-
print(tc, s);
59-
print(tc, System.lineSeparator());
59+
public long say(ThreadContext tc, String s) {
60+
long bytes = print(tc, s);
61+
bytes += print(tc, System.lineSeparator());
62+
return bytes;
6063
}
6164

6265
public void flush(ThreadContext tc) {

src/vm/jvm/runtime/org/perl6/nqp/io/SyncHandle.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,35 +199,37 @@ public byte[] read(ThreadContext tc, int bytes) {
199199
}
200200
}
201201

202-
public void write(ThreadContext tc, byte[] array) {
202+
public long write(ThreadContext tc, byte[] array) {
203203
ByteBuffer buffer = ByteBuffer.wrap(array);
204-
write(tc, buffer);
204+
return write(tc, buffer);
205205
}
206206

207-
protected void write(ThreadContext tc, ByteBuffer buffer) {
207+
protected long write(ThreadContext tc, ByteBuffer buffer) {
208208
try {
209209
int toWrite = buffer.limit();
210210
int written = 0;
211211
while (written < toWrite) {
212212
written += chan.write(buffer);
213213
}
214+
return written;
214215
} catch (IOException e) {
215216
throw ExceptionHandling.dieInternal(tc, e);
216217
}
217218
}
218219

219-
public void print(ThreadContext tc, String s) {
220+
public long print(ThreadContext tc, String s) {
220221
try {
221222
ByteBuffer buffer = enc.encode(CharBuffer.wrap(s));
222-
write(tc, buffer);
223+
return write(tc, buffer);
223224
} catch (IOException e) {
224225
throw ExceptionHandling.dieInternal(tc, e);
225226
}
226227
}
227228

228-
public void say(ThreadContext tc, String s) {
229-
print(tc, s);
230-
print(tc, System.lineSeparator());
229+
public long say(ThreadContext tc, String s) {
230+
long bytes = print(tc, s);
231+
bytes += print(tc, System.lineSeparator());
232+
return bytes;
231233
}
232234

233235
public void setInputLineSeparator(ThreadContext tc, String sep) {

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,15 @@ public static SixModelObject readfh(SixModelObject io, SixModelObject res, long
517517
}
518518
}
519519

520-
public static SixModelObject writefh(SixModelObject obj, SixModelObject buf, ThreadContext tc) {
520+
public static long writefh(SixModelObject obj, SixModelObject buf, ThreadContext tc) {
521521
ByteBuffer bb = decode8(buf, tc);
522+
long written;
522523
if (obj instanceof IOHandleInstance) {
523524
IOHandleInstance h = (IOHandleInstance)obj;
524525
byte[] bytesToWrite = new byte[bb.limit()];
525526
bb.get(bytesToWrite);
526527
if (h.handle instanceof IIOSyncWritable)
527-
((IIOSyncWritable)h.handle).write(tc, bytesToWrite);
528+
written = ((IIOSyncWritable)h.handle).write(tc, bytesToWrite);
528529
else
529530
throw ExceptionHandling.dieInternal(tc,
530531
"This handle does not support write");
@@ -533,14 +534,15 @@ public static SixModelObject writefh(SixModelObject obj, SixModelObject buf, Thr
533534
throw ExceptionHandling.dieInternal(tc,
534535
"writefh requires an object with the IOHandle REPR");
535536
}
536-
return buf;
537+
return written;
537538
}
538539

539-
public static String printfh(SixModelObject obj, String data, ThreadContext tc) {
540+
public static long printfh(SixModelObject obj, String data, ThreadContext tc) {
541+
long written;
540542
if (obj instanceof IOHandleInstance) {
541543
IOHandleInstance h = (IOHandleInstance)obj;
542544
if (h.handle instanceof IIOSyncWritable)
543-
((IIOSyncWritable)h.handle).print(tc, data);
545+
written = ((IIOSyncWritable)h.handle).print(tc, data);
544546
else
545547
throw ExceptionHandling.dieInternal(tc,
546548
"This handle does not support print");
@@ -549,14 +551,15 @@ public static String printfh(SixModelObject obj, String data, ThreadContext tc)
549551
throw ExceptionHandling.dieInternal(tc,
550552
"printfh requires an object with the IOHandle REPR");
551553
}
552-
return data;
554+
return written;
553555
}
554556

555-
public static String sayfh(SixModelObject obj, String data, ThreadContext tc) {
557+
public static long sayfh(SixModelObject obj, String data, ThreadContext tc) {
558+
long written;
556559
if (obj instanceof IOHandleInstance) {
557560
IOHandleInstance h = (IOHandleInstance)obj;
558561
if (h.handle instanceof IIOSyncWritable)
559-
((IIOSyncWritable)h.handle).say(tc, data);
562+
written = ((IIOSyncWritable)h.handle).say(tc, data);
560563
else
561564
throw ExceptionHandling.dieInternal(tc,
562565
"This handle does not support say");
@@ -565,7 +568,7 @@ public static String sayfh(SixModelObject obj, String data, ThreadContext tc) {
565568
throw ExceptionHandling.dieInternal(tc,
566569
"sayfh requires an object with the IOHandle REPR");
567570
}
568-
return data;
571+
return written;
569572
}
570573

571574
public static SixModelObject flushfh(SixModelObject obj, ThreadContext tc) {

0 commit comments

Comments
 (0)