Commits

camlspotter committed 57b8c6c

hmmm OO + Js_of_ocaml do not work well... yet

Comments (0)

Files changed (9)

       (** obtain OCaml string of repr *)
       val str_string : [> _Object ] t -> string
 	(** obtain OCaml string of str *)
+
+      val getByString : [> _Object ] t -> string -> _Object t
+        (** getItemString or getAttrString, depending on if the object is a dictionary or not *)
+
     end = struct
       let gen_call callable ?kwd list =
 	_internal_call callable (Tuple.from_list list) kwd
 
       let repr_string t = String.asString (repr t)
       let str_string t = String.asString (str t)
+
+      let getByString o = match Dict.coerce_opt o with
+        | None -> getAttrString o
+        | Some d -> Dict.getItemString d
     end
 
     include X
   let ( !:> ) o = (o :> _Object t)
   let ( $. ) = Object.getAttrString (* Python's o.x *)
   let ( $<> ) = Dict.getItemString (* Python's o[x] *)
-  let ( $ ) o = match Dict.coerce_opt o with
-    | None -> ($.) o
-    | Some d -> ($<>) d
+  let ( $ ) = Object.getByString
   let call o = Object.call (Callable.coerce o)
   let kwd_call o = Object.kwd_call (Callable.coerce o)
   let show_error typ detail = Printf.sprintf "%s: %s" (Py.String.asString (Object.str typ)) (Py.String.asString (Object.str detail))
 open Opycaml.Api
 
 let _ =
-  Base.initialize ();
-
   let string = Import.importModule "string" in (* from string import * *)
   let dict = Module.getDict string in
   let lowercase = Dict.getItemString dict "lowercase" in
      to avoid the name space collision with String in OCaml standard
      library. *)
   prerr_endline (Py.String.asString (Py.String.coerce res));
-
-  Base.finalize ()
 ;;

examples/testoo.ml

 open Opycaml.OOApi
 
 let _ =
-  Base.initialize ();
-
   let string = Import.importModule "string" in (* from string import * *)
   let dict = string#getDict in
   let lowercase = dict#getItemString "lowercase" in
   let res = Object.call (Callable.coerce capitalize) [lowercase] in
 
   prerr_endline (OPy.String.coerce res)#asString;
-
-  Base.finalize ()
 ;;

js_of_ocaml/OMakefile

 
 .DEFAULT: $(OCamlProgram test, py_of_ocaml opy_of_ocaml test)
 .DEFAULT: $(OCamlProgram testoo, py_of_ocaml opy_of_ocaml testoo)
+.DEFAULT: $(OCamlProgram testoox, opy_of_ocaml testoox)
 
 .PHONY: clean
 clean:

js_of_ocaml/opy_of_ocaml.ml

 end = struct
   type 'a t = o_Dict
   type 'a gen_prop
-  let dict = Obj.magic
+  let dict o = (o :> o_Dict)
   module Unsafe = struct
     let get (o : 'a t) name = Obj.magic (o#getItemString name)
     let meth_call (o : o_Dict) name args = 
-      Obj.magic (Eval.callObject (Callable.coerce (o#getItemString name)) (Array.to_list args ))
-    let inject = Obj.magic
+      Obj.magic (Object.call (Callable.coerce (o#getItemString name)) (Array.to_list args ))
+    let inject o = (o :> o_Object)
   end
 
   type 'a readonly_prop = < get : 'a> gen_prop
   type 'a meth
 end
 
+include Js
+
 let import_module name = Js.dict (Import.importModule name)#getDict

js_of_ocaml/py_of_ocaml.ml

     val inject : 'a OT.t -> OT._Object OT.t
   end
 end = struct
-  type 'a t = OT._Dict OT.t
+  type 'a t = OT._Object OT.t
   type 'a gen_prop
   let dict = Obj.magic
   module Unsafe = struct
-    let get (o : 'a t) name = 
-      Obj.magic (Dict.getItemString o name)
+    let get (o : 'a t) name = Obj.magic (o $ name)
     let meth_call (o : 'a t) name args = 
-      Obj.magic (Object.callObject (Callable.coerce (Dict.getItemString o name)) (Array.to_list args ))
+      Obj.magic (Object.call (Callable.coerce (o $ name)) (Array.to_list args ))
     let inject = Obj.magic
   end
 

js_of_ocaml/test.ml

 let import_module_dict name = Js.dict (Module.getDict (Import.importModule name))
 
 let _ =
-  Base.initialize ();
-
   let module_string : module_string Js.t = import_module_dict "string" in (* from string import * *)
   let lowercase = module_string##lowercase in
   let res = module_string##capitalize( lowercase ) in
      to avoid the name space collision with String in OCaml standard
      library. *)
   prerr_endline (Py.String.asString res);
-
-  Base.finalize ()
 ;;

js_of_ocaml/testoo.ml

 open Opy_of_ocaml
 
 class type module_string = object
-  method lowercase  : o_String Js.readonly_prop
-  method capitalize : #o_String -> o_String Js.meth
+  method lowercase  : o_String readonly_prop
+  method capitalize : #o_String -> o_String meth
 end
 
 let _ =
-  Base.initialize ();
-
-  let module_string : module_string Js.t = import_module "string" in (* from string import * *)
+  let module_string : module_string t = import_module "string" in (* from string import * *)
   let lowercase = module_string##lowercase in
-  let res = module_string##capitalize( lowercase ) in
+  (* CR jfuruse: Mmmm.... we need to create a new object. Obj.magic is not enough! We need a proper method table *) 
+  let res = OPy.String.coerce (module_string##capitalize( lowercase )) in
 
   prerr_endline res#asString;
-
-  Base.finalize ()
 ;;

js_of_ocaml/testoox.ml

+open Opycaml.OOApi
+open Opy_of_ocaml
+  
+class type module_string =
+  object
+    method lowercase : o_String Js.readonly_prop
+    method capitalize : #o_String -> o_String Js.meth
+  end
+  
+let _ =
+  let module_string : module_string Js.t = import_module "string" in
+  (* from string import * *)
+  let lowercase =
+    let module_string_1 : (< .. > as 'x1) Js.t = module_string in
+    let _ (arg1 : 'x1) =
+      (arg1#lowercase : < get : 'x2; .. > Js.gen_prop)
+    in 
+    (* (Js.Unsafe.get module_string_1 "lowercase" : 'x2)  *)
+    (Obj.magic module_string_1 : o_Dict)#getItemString "lowercase"
+  in
+
+  prerr_endline "lowercase";
+
+  let res =
+    let module_string_2 : (< .. > as 'x3) Js.t = module_string in
+    let _ (arg2 : 'x3) =
+      (arg2#capitalize : 'x4 -> 'x5 Js.meth)
+    in
+    OPy.String.coerce 
+      (Js.Unsafe.meth_call module_string_2 "capitalize" [| Js.Unsafe.inject ((Obj.magic lowercase) : 'x4) |] : 'x5)
+  in prerr_endline res#asString
+