[m-rev.] diff: fix java i/o problems

Peter Wang novalazy at gmail.com
Thu May 7 17:42:37 AEST 2009


Branches: main

Fix I/O problems in Java backend.

library/io.m:
        Don't write to System.out directly in io.write_*/3.  io.write_*/4
        works with a stream layered on top of System.out, and mixing the two
        sets of predicates results output coming out in the wrong order.

        In MR_MercuryFileStruct.put and MR_MercuryFileStruct.write flush a
        text output stream if a newline is written, otherwise the user is
        liable to not see any output at all (the buffering is very
        aggressive).

tests/hard_coded/csharp_test.m:
tests/hard_coded/java_test.m:
        Update some old :- pragma foreign_* syntax.

diff --git a/library/io.m b/library/io.m
index 4840f60..2c7d523 100644
--- a/library/io.m
+++ b/library/io.m
@@ -6056,6 +6056,9 @@ namespace mercury {
             try {
                 if (output != null) {
                     output.write(c);
+                    if (c == '\\n') {
+                        output.flush();
+                    }
                 } else if (randomaccess != null) {
                     randomaccess.write(c);
                 } else {
@@ -6082,12 +6085,17 @@ namespace mercury {

             try {
                 if (output != null) {
+                    int old_line_number = line_number;
                     for (int i = 0; i < s.length(); i++) {
                         if (s.charAt(i) == '\\n') {
                             line_number++;
                         }
                     }
                     output.write(s);
+                    /* Flush if we saw a newline. */
+                    if (old_line_number != line_number) {
+                        output.flush();
+                    }
                 } else {
                     for (int i = 0; i < s.length(); i++) {
                         // lower 8 bits of each
@@ -7667,25 +7675,25 @@ io.write_bitmap(Bitmap, Start, NumBytes, !IO) :-
     io.write_string(Message::in, _IO0::di, _IO::uo),
     [may_call_mercury, promise_pure, thread_safe, tabled_for_io, terminates],
 "
-    System.out.print(Message);
+    mercury_current_text_output.write(Message);
 ").
 :- pragma foreign_proc("Java",
-    io.write_char(Character::in, _IO0::di, _IO::uo),
+    io.write_char(Chr::in, _IO0::di, _IO::uo),
     [may_call_mercury, promise_pure, thread_safe, tabled_for_io, terminates],
 "
-    System.out.print(Character);
+    mercury_current_text_output.write(java.lang.Character.toString(Chr));
 ").
 :- pragma foreign_proc("Java",
     io.write_int(Val::in, _IO0::di, _IO::uo),
     [may_call_mercury, promise_pure, thread_safe, tabled_for_io, terminates],
 "
-    System.out.print(Val);
+    mercury_current_text_output.write(java.lang.Integer.toString(Val));
 ").
 :- pragma foreign_proc("Java",
     io.write_float(Val::in, _IO0::di, _IO::uo),
     [may_call_mercury, promise_pure, thread_safe, tabled_for_io, terminates],
 "
-    System.out.print(Val);
+    mercury_current_text_output.write(java.lang.Double.toString(Val));
 ").

 :- pragma foreign_proc("Java",
@@ -8776,7 +8784,7 @@
io.set_binary_output_stream(binary_output_stream(NewStream),
 ").

 :- pragma foreign_proc("Java",
-    io.stdin_stream(Stream::out, _IO0::di, _IO::uo),
+    io.stdin_stream_2(Stream::out, _IO0::di, _IO::uo),
     [will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
     Stream = mercury_stdin;
diff --git a/tests/hard_coded/csharp_test.m b/tests/hard_coded/csharp_test.m
index 14706ca..0d3ceb6 100644
--- a/tests/hard_coded/csharp_test.m
+++ b/tests/hard_coded/csharp_test.m
@@ -11,17 +11,17 @@
 main -->
 	csharp_write_string("Hello, world\n").

-:- pragma(foreign_decl, "C#", "
+:- pragma foreign_decl("C#", "
 	// some C Sharp declarations
 ").

-:- pragma(foreign_code, "C#", "
+:- pragma foreign_code("C#", "
 	// some C Sharp code
 ").

 :- pred csharp_write_string(string::in, io__state::di, io__state::uo) is det.

-:- pragma(foreign_proc, "C#",
+:- pragma foreign_proc("C#",
 	csharp_write_string(Message::in, _IO0::di, _IO::uo),
 	[will_not_call_mercury, promise_pure],
 "
diff --git a/tests/hard_coded/java_test.m b/tests/hard_coded/java_test.m
index 1705c66..7128200 100644
--- a/tests/hard_coded/java_test.m
+++ b/tests/hard_coded/java_test.m
@@ -29,12 +29,12 @@ main -->
 		[]
 	).

-:- pragma(foreign_decl, "Java", "
+:- pragma foreign_decl("Java", "
 	// some Java top-level declarations
 	class Foo {}
 ").

-:- pragma(foreign_code, "Java", "
+:- pragma foreign_code("Java", "
 	// some Java in-class declarations
 	static void bar() {
 /*
@@ -49,7 +49,7 @@ XXX `pragma export' not yet supported for Java

 :- pred java_write_string(string::in, io__state::di, io__state::uo) is det.

-:- pragma(foreign_proc, "Java",
+:- pragma foreign_proc("Java",
 	java_write_string(Message::in, _IO0::di, _IO::uo),
 	[will_not_call_mercury, promise_pure],
 "
@@ -63,7 +63,7 @@ XXX `pragma export' not yet supported for Java

 :- pred java_semidet_succeed is semidet.
 :- pred java_semidet_fail is semidet.
-:- pragma(foreign_proc, "Java", java_semidet_succeed,
+:- pragma foreign_proc("Java", java_semidet_succeed,
 	[will_not_call_mercury, promise_pure], "succeeded = true;").
-:- pragma(foreign_proc, "Java", java_semidet_fail,
+:- pragma foreign_proc("Java", java_semidet_fail,
 	[will_not_call_mercury, promise_pure], "succeeded = false;").
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list