[m-rev.] For review: Java implementation of builtin library
James Goddard
goddardjames at yahoo.com
Tue Jan 20 12:21:49 AEDT 2004
Estimated hours taken: 4
Branches: main
Implement some library procedures for the Java back-end.
library/builtin.m:
Implement the following functions:
cc_cast/1
cc_cast_io/1
Partially implemented - This procedure serves as little more than
a place-holder for the moment. (See comment in deep_copy()).
copy/2
Index: builtin.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/builtin.m,v
retrieving revision 1.100
diff -u -d -r1.100 builtin.m
--- builtin.m 1 Dec 2003 13:17:10 -0000 1.100
+++ builtin.m 20 Jan 2004 01:18:51 -0000
@@ -398,6 +398,19 @@
"
Y = X;
").
+:- pragma foreign_proc("Java",
+ cc_cast(X :: (pred(out) is cc_multi)) = (Y :: out(pred(out) is det)),
+ [will_not_call_mercury, thread_safe],
+"
+ Y = X;
+").
+:- pragma foreign_proc("Java",
+ cc_cast(X :: (pred(out) is cc_nondet)) =
+ (Y :: out(pred(out) is semidet)),
+ [will_not_call_mercury, thread_safe],
+"
+ Y = X;
+").
:- pragma promise_pure(promise_only_solution_io/4).
promise_only_solution_io(Pred, X) -->
@@ -425,6 +438,13 @@
"
Y = X;
").
+:- pragma foreign_proc("Java",
+ cc_cast_io(X :: (pred(out, di, uo) is cc_multi)) =
+ (Y :: out(pred(out, di, uo) is det)),
+ [will_not_call_mercury, thread_safe],
+"
+ Y = X;
+").
%-----------------------------------------------------------------------------%
@@ -618,6 +638,57 @@
").
+:- pragma foreign_code("Java",
+"
+public static java.lang.Object
+deep_copy(java.lang.Object original) {
+ java.lang.Object clone;
+
+ if (original == null) {
+ return null;
+ }
+
+ java.lang.Class cls = original.getClass();
+
+ if (cls.getName().equals(""java.lang.String"")) {
+ return new java.lang.String((java.lang.String) original);
+ }
+
+ if (cls.isArray()) {
+ int length = java.lang.reflect.Array.getLength(original);
+ clone = java.lang.reflect.Array.newInstance(
+ cls.getComponentType(), length);
+ for (int i = 0; i < length; i++) {
+ java.lang.Object X, Y;
+ X = java.lang.reflect.Array.get(original, i);
+ Y = deep_copy(X);
+ java.lang.reflect.Array.set(clone, i, Y);
+ }
+ return clone;
+ }
+
+ /*
+ ** XXX Two possible approaches are possible here:
+ **
+ ** 1. Get all mercury objects to implement the Serializable interface.
+ ** Then this whole function could be replaced with code that writes
+ ** the Object out via an ObjectOutputStream into a byte array (or
+ ** something), then reads it back in again, thus creating a copy.
+ ** 2. Call cls.getConstructors(), then iterate through the resulting
+ ** array until one of them allows instantiation with all parameters
+ ** set to 0 or null (or some sort of recursive call that attempts to
+ ** instantiate the parameters).
+ ** This approach is of course not guaranteed to work all the time.
+ ** Then we can just copy the fields across using Reflection.
+ **
+ ** For now, we're just throwing an exception.
+ */
+
+ throw new java.lang.RuntimeException(
+ ""deep copy not yet fully implemented"");
+}
+").
+
%-----------------------------------------------------------------------------%
% unsafe_promise_unique is a compiler builtin.
@@ -650,6 +721,20 @@
").
:- pragma foreign_proc("C#",
+ copy(X::in, Y::uo),
+ [may_call_mercury, thread_safe, promise_pure],
+"
+ Y = deep_copy(X);
+").
+
+:- pragma foreign_proc("Java",
+ copy(X::ui, Y::uo),
+ [may_call_mercury, thread_safe, promise_pure],
+"
+ Y = deep_copy(X);
+").
+
+:- pragma foreign_proc("Java",
copy(X::in, Y::uo),
[may_call_mercury, thread_safe, promise_pure],
"
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list