Commits

camlspotter  committed 8223dff

js_of_ocaml example

  • Participants
  • Parent commits f59072e

Comments (0)

Files changed (6)

 
 .SUBDIRS: tests
 
+

File js_of_ocaml/OMakefile

+USE_OCAMLFIND=true
+
+OCAMLPACKS = opycaml
+
+OCAMLDEPFLAGS += -syntax camlp4o -package js_of_ocaml.syntax
+OCAMLPPFLAGS += -syntax camlp4o -package js_of_ocaml.syntax
+OCAMLFLAGS += -annot
+
+.DEFAULT: $(OCamlProgram test, py_of_ocaml test)
+
+.PHONY: clean
+clean:
+  rm -f $(filter-proper-targets $(ls R, .))

File js_of_ocaml/OMakeroot

+########################################################################
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this file, to deal in the File without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the File, and to permit persons to whom the
+# File is furnished to do so, subject to the following condition:
+#
+# THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE FILE OR
+# THE USE OR OTHER DEALINGS IN THE FILE.
+
+########################################################################
+# The standard OMakeroot file.
+# You will not normally need to modify this file.
+# By default, your changes should be placed in the
+# OMakefile in this directory.
+#
+# If you decide to modify this file, note that it uses exactly
+# the same syntax as the OMakefile.
+#
+
+#
+# Include the standard installed configuration files.
+# Any of these can be deleted if you are not using them,
+# but you probably want to keep the Common file.
+#
+open build/C
+open build/OCaml
+open build/LaTeX
+
+#
+# The command-line variables are defined *after* the
+# standard configuration has been loaded.
+#
+DefineCommandVars()
+
+#
+# Include the OMakefile in this directory.
+#
+.SUBDIRS: .

File js_of_ocaml/README.txt

+This is a test of using js_of_ocaml syntax sugar in OPyCaml. Seems nice!!!

File js_of_ocaml/py_of_ocaml.ml

+open Opycaml.Api
+
+module OT = Opycaml.Type
+
+module Js : sig
+  type 'a t
+  type 'a gen_prop
+  type 'a readonly_prop = < get : 'a> gen_prop
+  type 'a meth
+  val dict : OT._Dict OT.t -> 'a t
+  module Unsafe : sig
+    val get : 'a t -> string -> 'b OT.t
+    val meth_call : 'a t -> string -> OT._Object OT.t array -> 'b OT.t
+    val inject : 'a OT.t -> OT._Object OT.t
+  end
+end = struct
+  type 'a t = OT._Dict OT.t
+  type 'a gen_prop
+  let dict (d : OT._Dict OT.t) : 'a t = Obj.magic d
+  module Unsafe = struct
+    let get (o : 'a t) name = 
+      Obj.magic (Dict.getItemString o name)
+    let meth_call (o : 'a t) name args = 
+      Obj.magic (Object.callObject (Callable.coerce (Dict.getItemString o name)) (Array.to_list args ))
+    let inject = Obj.magic
+  end
+
+  type 'a readonly_prop = < get : 'a> gen_prop
+  type 'a meth
+end

File js_of_ocaml/test.ml

+(* open Opycaml.Api is recommended. *)
+open Opycaml.Api
+open Py_of_ocaml
+
+class type module_string = object
+  method lowercase  : _String t Js.readonly_prop
+  method capitalize : [>_String] t -> _String t Js.meth
+end
+
+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
+
+  (* String class is not accessible by String but Py.String, in order
+     to avoid the name space collision with String in OCaml standard
+     library. *)
+  prerr_endline (Py.String.asString res);
+
+  Base.finalize ()
+;;