Files
mercury/tests/hard_coded/java_test.m
Peter Wang c2b1704378 Fix I/O problems in Java backend.
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.
2009-05-07 07:42:16 +00:00

70 lines
1.6 KiB
Mathematica

% A test of the Java interface.
:- module java_test.
:- interface.
:- import_module io.
:- pred main(io__state::di, io__state::uo) is det.
:- implementation.
:- import_module int.
:- func foo(int) = int.
/*
XXX `pragma export' not yet supported for Java
:- pragma export(foo(in) = out, "foo").
*/
foo(X) = X + 1.
main -->
java_write_string("Hello, world\n"),
( { java_semidet_succeed } ->
[]
;
java_write_string("java_semidet_succeed failed\n")
),
( { java_semidet_fail } ->
java_write_string("java_semidet_fail succeeded\n")
;
[]
).
:- pragma foreign_decl("Java", "
// some Java top-level declarations
class Foo {}
").
:- pragma foreign_code("Java", "
// some Java in-class declarations
static void bar() {
/*
XXX `pragma export' not yet supported for Java
// test `pragma export' functions
if (foo(42) != 43) {
throw new java.lang.Error(""bar: foo failed"");
}
*/
}
").
:- pred java_write_string(string::in, io__state::di, io__state::uo) is det.
:- pragma foreign_proc("Java",
java_write_string(Message::in, _IO0::di, _IO::uo),
[will_not_call_mercury, promise_pure],
"
// a Java procedure
System.out.print(Message);
// test that foreign_decl declarations are visible
Foo f;
// test that foreign_code declarations are visible
bar();
").
:- pred java_semidet_succeed is semidet.
:- pred java_semidet_fail is semidet.
:- pragma foreign_proc("Java", java_semidet_succeed,
[will_not_call_mercury, promise_pure], "succeeded = true;").
:- pragma foreign_proc("Java", java_semidet_fail,
[will_not_call_mercury, promise_pure], "succeeded = false;").