camlspotter avatar camlspotter committed d076117 Draft

java main => ocaml => java => ocaml

Comments (0)

Files changed (5)

Add a comment to this file

camljava-0.3/test_main_in_java/Main.jar

Binary file added.

camljava-0.3/test_main_in_java/Makefile

-all: Main.class libOCaml.so
-	LD_LIBRARY_PATH=.:`ocamlfind printconf destdir`/camljava CLASSPATH=`ocamlfind printconf destdir`/camljava/camljava.jar:. java Main
+all: Main.class Test.class Testcb.class libOCaml.so
+	LD_LIBRARY_PATH=.:`ocamlfind printconf destdir`/camljava CLASSPATH=`ocamlfind printconf destdir`/camljava/camljava.jar:. java Main Test Testcb
 
-Main.class: Main.java
-	CLASSPATH=`ocamlfind printconf destdir`/camljava/camljava.jar javac Main.java
+.SUFFIXES: .c .h .o .java .class .ml .cmo
+
+.java.class:
+	CLASSPATH=`ocamlfind printconf destdir`/camljava/camljava.jar:. javac $<
 
 Main.h: Main.class
 	javah -jni Main
 Main.o: Main.h Main.c
 	gcc -c -I $(JAVA)/include -I `ocamlc -where` Main.c
 
-libOCaml.so: Makefile hello.o
-	ld -shared  hello.o -L`ocamlc -where` -L`ocamlfind printconf destdir`/camljava -lcamljni_shared -lcamlrun -ldl -lm -lncurses -o $@ 
+libOCaml.so: Makefile cmo.o
+	ld -shared cmo.o -L`ocamlc -where` -L`ocamlfind printconf destdir`/camljava -lcamljni_shared -lcamlrun -ldl -lm -lncurses -o $@ 
 
-hello.o: hello.ml
-	ocamlc -output-obj $< -o $@
+.ml.cmo:
+	ocamlfind ocamlc -package camljava -c $< -o $@
+
+cmo.o: hello.cmo jnitest.cmo
+	ocamlc -output-obj -o $@ -I `ocamlfind printconf destdir`/camljava jni.cma hello.cmo jnitest.cmo 
 
 clean:
 	rm -f *.o *.so *.class Main.h *.cm*
 
-
-

camljava-0.3/test_main_in_java/Test.java

+class Test {
+  static void f()
+  {
+    System.out.println("f"); 
+  }
+  static int g(int x, int y)
+  {
+    System.out.println("g " + x + " " + y); 
+    return x + y;
+  }
+  static int a;
+  int b;
+  int h() {
+    System.out.println("h");
+    return b;
+  }
+  static int k(Testcb cb, int x)
+  {
+    System.out.println("k " + x);
+    cb.f();
+    return cb.g(x); 
+  }
+}

camljava-0.3/test_main_in_java/Testcb.java

+import fr.inria.caml.camljava.Callback;
+
+class Testcb {
+    private Callback cb;
+    private static long _f = Callback.getCamlMethodID("f");
+    private static long _g = Callback.getCamlMethodID("g");
+    public Testcb(Callback c) { cb = c; }
+    public void f()
+    { Object[] args = { }; cb.callVoid(_f, args); }
+    public int g(int x)
+    { Object[] args = { new fr.inria.caml.camljava.Camlint(x) };
+      return cb.callCamlint(_g, args); }
+}

camljava-0.3/test_main_in_java/jnitest.ml

+open Jni
+
+class cls = object
+  method f = print_string "cls.f called"; print_newline()
+  method g x =
+    print_string "cls.g called with "; print_int x; print_newline();
+    x+42
+end
+
+let wrap_caml_object() =
+  let camlobj = new cls in
+  (* Wrap caml object into instance of Testcb (see Testcb.java) *)
+  let c = find_class "Testcb" in
+  let i = get_methodID c "<init>" "(Lfr/inria/caml/camljava/Callback;)V" in
+  let o = alloc_object c in
+  call_nonvirtual_void_method o c i [|Obj(wrap_object camlobj)|];
+  o
+
+let test() =
+  (* Static method invocation *)
+  let c = find_class "Test" in
+  let f = get_static_methodID c "f" "()V" in
+  print_string "Calling Test.f()"; print_newline();
+  call_static_void_method c f [||];
+  let g = get_static_methodID c "g" "(II)I" in
+  print_string "Calling Test.g(12,45)"; print_newline();
+  let r = call_static_int_method c g [|Camlint 12; Camlint 45|] in
+  print_string "Result is: "; print_string (Int32.to_string r); 
+  print_newline();
+  (* Static field access *)
+  let a = get_static_fieldID c "a" "I" in
+  print_string "Current value of Test.a is: ";
+  print_string (Int32.to_string (get_static_int_field c a));
+  print_newline();
+  print_string "Setting Test.a to 12"; print_newline();
+  set_static_int_field c a (Int32.of_int 12);
+  print_string "Current value of Test.a is: ";
+  print_string (Int32.to_string (get_static_int_field c a));
+  print_newline();
+  (* Object creation *)
+  print_string "Creating an instance of Test..."; print_newline();
+  let o = alloc_object c in
+  let init = get_methodID c "<init>" "()V" in
+  call_nonvirtual_void_method o c init [||];
+  (* Virtual method invocation *)
+  let h = get_methodID c "h" "()I" in
+  print_string "Calling testinstance.h()"; print_newline();
+  let r = call_int_method o h [||] in
+  print_string "Result is: "; print_string (Int32.to_string r); 
+  print_newline();
+  (* Instance field access *)
+  let b = get_fieldID c "b" "I" in
+  print_string "Setting testinstance.b to 45"; print_newline();
+  set_int_field o b (Int32.of_int 45);
+  print_string "Calling testinstance.h()"; print_newline();
+  let r = call_int_method o h [||] in
+  print_string "Result is: "; print_string (Int32.to_string r); 
+  print_newline();
+  print_string "Current value of testinstance.b is: ";
+  print_string (Int32.to_string (get_int_field o b));
+  print_newline();
+  print_string "Wrapping Caml object into Java object..."; print_newline();
+  let cb = wrap_caml_object() in
+  let k = get_static_methodID c "k" "(LTestcb;I)I" in
+  print_string "Calling Test.k(<caml object>, 2)"; print_newline();
+  let r = call_static_int_method c k [|Obj cb; Camlint 2|] in
+  print_string "Result is: "; print_string (Int32.to_string r); 
+  print_newline()
+
+let _ =
+  test()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.