diff --git a/.gitignore b/.gitignore index c8f0021..e35d885 100644 --- a/.gitignore +++ b/.gitignore @@ -1,32 +1 @@ _build -*.byte -*.docdir - -tests/*.cmi -tests/*.cmo -tests/*.cmt -tests/*.out - -examples/*.cmi -examples/*.cmo -examples/*.cmt -examples/*.out - -setup.data -setup.log -notes.txt -scratch.js -server.js -exhaust_api.js -.tern-port -chat_server.js -examples/node_modules -package.json -junk.ml -*.js - -examples/buffer -examples/file_system -examples_lwt -old_readme -node_modules \ No newline at end of file diff --git a/.merlin b/.merlin deleted file mode 100644 index 504c423..0000000 --- a/.merlin +++ /dev/null @@ -1,4 +0,0 @@ -S src -B _build/src -PKG js_of_ocaml js_of_ocaml.ppx nodejs nodejs.high_level nodejs.high_level_lwt -FLG -w +a-4-40..42-44-45-48 diff --git a/.ocamlformat b/.ocamlformat new file mode 100644 index 0000000..f946cc0 --- /dev/null +++ b/.ocamlformat @@ -0,0 +1,43 @@ +version=0.25.1 +assignment-operator=end-line +break-cases=fit +break-fun-decl=wrap +break-fun-sig=wrap +break-infix=wrap +break-infix-before-func=false +break-separators=before +break-sequences=true +cases-exp-indent=2 +cases-matching-exp-indent=normal +doc-comments=before +doc-comments-padding=2 +doc-comments-tag-only=default +dock-collection-brackets=false +exp-grouping=preserve +field-space=loose +if-then-else=compact +indicate-multiline-delimiters=space +indicate-nested-or-patterns=unsafe-no +infix-precedence=indent +leading-nested-match-parens=false +let-and=sparse +let-binding-spacing=compact +let-module=compact +margin=80 +max-indent=2 +module-item-spacing=sparse +ocaml-version=4.14.0 +ocp-indent-compat=false +parens-ite=false +parens-tuple=always +parse-docstrings=true +sequence-blank-line=preserve-one +sequence-style=terminator +single-case=compact +space-around-arrays=true +space-around-lists=true +space-around-records=true +space-around-variants=true +type-decl=sparse +wrap-comments=false +wrap-fun-args=true diff --git a/.tern-project b/.tern-project deleted file mode 100644 index ae87470..0000000 --- a/.tern-project +++ /dev/null @@ -1,6 +0,0 @@ -{ - "plugins": { - "node": { - } - } -} \ No newline at end of file diff --git a/Makefile b/Makefile deleted file mode 100644 index 3639f14..0000000 --- a/Makefile +++ /dev/null @@ -1,41 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: a3c674b4239234cbbe53afe090018954) - -SETUP = ocaml setup.ml - -build: setup.data - $(SETUP) -build $(BUILDFLAGS) - -doc: setup.data build - $(SETUP) -doc $(DOCFLAGS) - -test: setup.data build - $(SETUP) -test $(TESTFLAGS) - -all: - $(SETUP) -all $(ALLFLAGS) - -install: setup.data - $(SETUP) -install $(INSTALLFLAGS) - -uninstall: setup.data - $(SETUP) -uninstall $(UNINSTALLFLAGS) - -reinstall: setup.data - $(SETUP) -reinstall $(REINSTALLFLAGS) - -clean: - $(SETUP) -clean $(CLEANFLAGS) - -distclean: - $(SETUP) -distclean $(DISTCLEANFLAGS) - -setup.data: - $(SETUP) -configure $(CONFIGUREFLAGS) - -configure: - $(SETUP) -configure $(CONFIGUREFLAGS) - -.PHONY: build doc test all install uninstall reinstall clean distclean configure - -# OASIS_STOP diff --git a/README.markdown b/README.md similarity index 100% rename from README.markdown rename to README.md diff --git a/_oasis b/_oasis deleted file mode 100644 index b670b8f..0000000 --- a/_oasis +++ /dev/null @@ -1,52 +0,0 @@ -# -*- conf -*- -OASISFormat: 0.4 -Name: nodejs -Version: 1.0.0 -Synopsis: js_of_ocaml bindings for nodejs -Authors: Edgar Aroutiounian -Maintainers: Edgar Aroutiounian -Homepage: https://github.com/fxfactorial/ocaml-nodejs -License: BSD-3-clause -Plugins: META (0.4), DevFiles (0.4) -BuildTools: ocamlbuild - -Description: - Write OCaml, run on node - - These are js_of_ocaml bindings to the node JavaScript API. - Get all the power of the node ecosystem with the type safety - of OCaml. - -Library nodejs - Path: src - Modules: Nodejs - CompiledObject: byte - ByteOpt: -g -w +a-4-40..42-44-45-48 - BuildDepends: - js_of_ocaml (>= 2.8.1), - js_of_ocaml.ppx, - lwt.ppx, - lwt (>= 2.5.2) - -Library high_level - FindlibName: high_level - FindlibParent: nodejs - Path: src/higher_level - Modules: Nodejs_high_level - CompiledObject: byte - ByteOpt: -g -w +a-4-40..42-44-45-48 - BuildDepends: nodejs - -Library high_level_lwt - FindlibName: high_level_lwt - FindlibParent: nodejs - Path: src/higher_level_lwt - Modules: Nodejs_high_level_lwt - CompiledObject: byte - ByteOpt: -g -w +a-4-40..42-44-45-48 - BuildDepends: nodejs.high_level - -SourceRepository master - Type: git - Location: https://github.com/fxfactorial/ocaml-nodejs.git - Browser: https://github.com/fxfactorial/ocaml-nodejs \ No newline at end of file diff --git a/_tags b/_tags deleted file mode 100644 index 5849663..0000000 --- a/_tags +++ /dev/null @@ -1,46 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 489249b49b97e38712418115aed3c29e) -# Ignore VCS directories, you can use the same kind of rule outside -# OASIS_START/STOP if you want to exclude directories that contains -# useless stuff for the build process -true: annot, bin_annot -<**/.svn>: -traverse -<**/.svn>: not_hygienic -".bzr": -traverse -".bzr": not_hygienic -".hg": -traverse -".hg": not_hygienic -".git": -traverse -".git": not_hygienic -"_darcs": -traverse -"_darcs": not_hygienic -# Library nodejs -"src/nodejs.cmxs": use_nodejs -"src/nodejs.cma": oasis_library_nodejs_byte -: oasis_library_nodejs_byte -: pkg_js_of_ocaml -: pkg_js_of_ocaml.ppx -: pkg_lwt -: pkg_lwt.ppx -# Library high_level -"src/higher_level/high_level.cmxs": use_high_level -"src/higher_level/high_level.cma": oasis_library_high_level_byte -: oasis_library_high_level_byte -: pkg_js_of_ocaml -: pkg_js_of_ocaml.ppx -: pkg_lwt -: pkg_lwt.ppx -: use_nodejs -# Library high_level_lwt -"src/higher_level_lwt/high_level_lwt.cmxs": use_high_level_lwt -"src/higher_level_lwt/high_level_lwt.cma": oasis_library_high_level_lwt_byte -: oasis_library_high_level_lwt_byte -: pkg_js_of_ocaml -: pkg_js_of_ocaml.ppx -: pkg_lwt -: pkg_lwt.ppx -: use_high_level -: use_nodejs -# OASIS_STOP -"examples": not_hygienic -"examples_lwt": not_hygienic diff --git a/configure b/configure deleted file mode 100755 index 6acfaeb..0000000 --- a/configure +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh - -# OASIS_START -# DO NOT EDIT (digest: dc86c2ad450f91ca10c931b6045d0499) -set -e - -FST=true -for i in "$@"; do - if $FST; then - set -- - FST=false - fi - - case $i in - --*=*) - ARG=${i%%=*} - VAL=${i##*=} - set -- "$@" "$ARG" "$VAL" - ;; - *) - set -- "$@" "$i" - ;; - esac -done - -ocaml setup.ml -configure "$@" -# OASIS_STOP diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..237a90b --- /dev/null +++ b/dune-project @@ -0,0 +1,29 @@ +(lang dune 3.0) + +(name nodejs) + +(license BSD-3-clause) + +(authors + "Edgar Aroutiounnian ") + +(maintainers + "Edgar Aroutiounnian ") + +(source + (github fxfactorial/ocaml-nodejs)) + +(generate_opam_files true) + +(package + (name nodejs) + (synopsis + "js_of_ocaml bindings for nodejs") + (description + "Write OCaml, run on node; these are js_of_ocaml bindings to the node +JavaScript API. Get all the power of the node ecosystem with the type +safety of OCaml.") + (depends + ocaml + (js_of_ocaml + (>= 2.5.2)))) diff --git a/examples/Makefile b/examples/Makefile deleted file mode 100644 index 631dcca..0000000 --- a/examples/Makefile +++ /dev/null @@ -1,46 +0,0 @@ -pkgs := nodejs.high_level,js_of_ocaml.ppx -js_debug := --debug-info --no-inline --pretty - -all:buffer file_system high_level events dns net os_high_level - -buffer:buffer_example.ml - ocamlfind ocamlc -g -package ${pkgs} -linkpkg $< -o $@ - js_of_ocaml ${js_debug} $@ -o $@.js - node $@.js - -file_system:file_system.ml - ocamlfind ocamlc -g -package ${pkgs} -linkpkg $< -o $@ - js_of_ocaml ${js_debug} $@ -o $@.js - node $@.js - -high_level:high_level.ml - ocamlfind ocamlc -g -package ${pkgs} -linkpkg $< -o $@ - js_of_ocaml ${js_debug} $@ -o $@.js - node $@.js - -events:events_example.ml - ocamlfind ocamlc -g -package ${pkgs} -linkpkg $< -o $@ - js_of_ocaml ${js_debug} $@ -o $@.js - node $@.js - -dns:dns_example.ml - ocamlfind ocamlc -g -package ${pkgs} -linkpkg $< -o $@ - js_of_ocaml ${js_debug} $@ -o $@.js - node $@.js - -net:net_example.ml - ocamlfind ocamlc -g -package ${pkgs} -linkpkg $< -o $@ - js_of_ocaml ${js_debug} $@ -o $@.js - node $@.js - -os_high_level:os_example.ml - ocamlfind ocamlc -g -package ${pkgs} -linkpkg $< -o $@ - js_of_ocaml ${js_debug} $@ -o $@.js - node $@.js - -clean: - @rm -f *.cmi *.cmo *.cmt a.out *.js \ - buffer file_system high_level events \ - dns net os_high_level - -.PHONY:clean file_system buffer dns events net os_high_level diff --git a/examples/buffer_example.ml b/examples/buffer.ml similarity index 89% rename from examples/buffer_example.ml rename to examples/buffer.ml index a7bacc7..7915ad6 100644 --- a/examples/buffer_example.ml +++ b/examples/buffer.ml @@ -1,3 +1,4 @@ +open Js_of_ocaml let () = let item = Nodejs.Buffer.buffer_static##from (Js.string "Hello World") in diff --git a/examples/dns_example.ml b/examples/dns.ml similarity index 86% rename from examples/dns_example.ml rename to examples/dns.ml index 0145004..713350e 100644 --- a/examples/dns_example.ml +++ b/examples/dns.ml @@ -1,4 +1,3 @@ - (* let () = *) (* Nodejs.Dns.dns##resolve4 *) (* (Js.string "google.com") *) @@ -8,5 +7,4 @@ (* arr *) (* |> Js.str_array |> Js.to_array *) (* |> Array.map Js.to_string |> Array.to_list *) - (* |> List.iter print_endline)) *) - +(* |> List.iter print_endline)) *) diff --git a/examples/dune b/examples/dune new file mode 100644 index 0000000..efeb577 --- /dev/null +++ b/examples/dune @@ -0,0 +1,77 @@ +(executables + (names buffer file_system events dns net) + (modules buffer file_system events dns net) + (libraries js_of_ocaml nodejs) + (preprocess + (pps js_of_ocaml-ppx)) + (modes js)) + +(executable + (name high_level) + (modules high_level) + (libraries js_of_ocaml nodejs nodejs_high_level) + (preprocess + (pps js_of_ocaml-ppx)) + (modes js)) + +(executable + (name os) + (modules os) + (libraries js_of_ocaml nodejs nodejs_high_level) + (preprocess + (pps js_of_ocaml-ppx)) + (modes js)) + +(rule + (alias runbuffer) + (deps buffer.bc.js) + (action + (run node %{deps}))) + +(rule + (alias runfile_system) + (deps file_system.bc.js example.json) + (action + (run node file_system.bc.js))) + +(rule + (alias runhigh_level) + (deps high_level.bc.js example.json) + (action + (run node %{deps}))) + +(rule + (alias runevents) + (deps events.bc.js) + (action + (run node %{deps}))) + +(rule + (alias rundns) + (deps dns.bc.js) + (action + (run node %{deps}))) + +(rule + (alias runnet) + (deps net.bc.js) + (action + (run node %{deps}))) + +(rule + (alias runos) + (deps os.bc.js) + (action + (run node %{deps}))) + +(executable + (name file_system_lwt) + (modules file_system_lwt) + (libraries nodejs_high_level_lwt) + (modes js)) + +(rule + (alias runfile_system_lwt) + (deps file_system_lwt.bc.js) + (action + (run node %{deps}))) diff --git a/examples/events_example.ml b/examples/events.ml similarity index 62% rename from examples/events_example.ml rename to examples/events.ml index bef5b01..eb04db8 100644 --- a/examples/events_example.ml +++ b/examples/events.ml @@ -1,16 +1,15 @@ - +open Js_of_ocaml let () = let e = new%js Nodejs.Events.event_emitter in - e##addListener - (Js.string "eventOne") + e##addListener (Js.string "eventOne") (Js.wrap_callback (fun () -> print_endline "Called in the event callback")); let b = (* Add this to jsoo api itself *) - e##eventNames |> Js.str_array |> Js.to_array - |> Array.map Js.to_string |> Array.to_list + e##eventNames |> Js.str_array |> Js.to_array |> Array.map Js.to_string + |> Array.to_list in List.iter print_endline b; diff --git a/examples/file_system.ml b/examples/file_system.ml index ad5e5d8..2c4febb 100644 --- a/examples/file_system.ml +++ b/examples/file_system.ml @@ -1,17 +1,17 @@ +open Js_of_ocaml + let () = let result = - Nodejs.Fs.fs##readFileSync - (Js.string "example.json") + Nodejs.Fs.fs##readFileSync (Js.string "example.json") (object%js - val encoding = Js.null - val flag = Js.string "r" - end) + val encoding = Js.null + + val flag = Js.string "r" + end ) in result##toString |> Js.to_string |> print_endline let () = - Nodejs.Fs.fs##readFile - (Js.string "example.json") + Nodejs.Fs.fs##readFile (Js.string "example.json") (Js.wrap_callback (fun _ contents -> - contents##toString |> Js.to_string |> print_endline - )) + contents##toString |> Js.to_string |> print_endline ) ) diff --git a/examples/file_system_lwt.ml b/examples/file_system_lwt.ml new file mode 100644 index 0000000..32a3ae9 --- /dev/null +++ b/examples/file_system_lwt.ml @@ -0,0 +1,8 @@ +open Lwt.Infix + +let () = + Nodejs_high_level_lwt.Fs.read_file (Nodejs_high_level.__filename ()) + >>= (fun (_err, result) -> + result#to_string |> print_endline |> Lwt.return >|= fun () -> + print_endline "Finished Program and Ordered Async" ) + |> Lwt.ignore_result diff --git a/examples/high_level.ml b/examples/high_level.ml index 64021d0..a35cd13 100644 --- a/examples/high_level.ml +++ b/examples/high_level.ml @@ -3,8 +3,8 @@ open Nodejs_high_level let () = Fs.read_file_sync "example.json" Fs.Read |> print_endline; - Fs.read_file_async "example.json" - (fun err contents -> contents#to_string |> print_endline) + Fs.read_file_async "example.json" (fun _err contents -> + contents#to_string |> print_endline ) let () = let e = new Events.event_emmiter in @@ -15,8 +15,8 @@ let () = let () = let s = new Net.server (fun client -> - client#write "Welcome to the Matrix"; - print_endline "Client connected") + client#write "Welcome to the Matrix"; + print_endline "Client connected" ) in s#listen 8124 (fun () -> print_endline "Created a server") diff --git a/examples/net.ml b/examples/net.ml new file mode 100644 index 0000000..a7aa9d1 --- /dev/null +++ b/examples/net.ml @@ -0,0 +1,19 @@ +open Js_of_ocaml +open Nodejs + +let () = + let server = + Net.net##createServer_withConnListener + (Js.wrap_callback (fun c -> + print_endline "Client connected"; + + c##on_withNoArgs (Js.string "end") + (Js.wrap_callback (fun () -> print_endline "Client disconnected")); + + c##write (Buffer.buffer_static##from (Js.string "Hello\r\n")); + + c##pipe c |> ignore; + + () ) ) + in + server##listen 8124 (Js.wrap_callback (fun _ -> print_endline "Server bound")) diff --git a/examples/net_example.ml b/examples/net_example.ml deleted file mode 100644 index ffecdb5..0000000 --- a/examples/net_example.ml +++ /dev/null @@ -1,21 +0,0 @@ - -open Nodejs - -let () = - let server = Net.net##createServer_withConnListener - (Js.wrap_callback (fun c -> - - print_endline "Client connected"; - - c##on_withNoArgs - (Js.string "end") - (Js.wrap_callback (fun () -> print_endline "Client disconnected")); - - - c##write (Buffer.buffer_static##from (Js.string "Hello\r\n")); - - c##pipe c |> ignore; - - ())) - in - server##listen 8124 (Js.wrap_callback (fun _ -> print_endline "Server bound")) diff --git a/examples/os.ml b/examples/os.ml new file mode 100644 index 0000000..2f86274 --- /dev/null +++ b/examples/os.ml @@ -0,0 +1,7 @@ +let () = + Nodejs_high_level.Os.cpu_stats () + |> List.iter (fun cpu -> + let open Nodejs_high_level.Os in + Printf.sprintf "model %s, speed: %d, times: idle: %d, sys: %d, user: %d" + cpu.model cpu.speed cpu.times#idle cpu.times#idle cpu.times#user + |> print_endline ) diff --git a/examples/os_example.ml b/examples/os_example.ml deleted file mode 100644 index 34276bf..0000000 --- a/examples/os_example.ml +++ /dev/null @@ -1,8 +0,0 @@ - -let () = - Nodejs_high_level.Os.cpu_stats () - |> List.iter (fun cpu -> - let open Nodejs_high_level.Os in - Printf.sprintf "model %s, speed: %d, times: idle: %d, sys: %d, user: %d" - cpu.model cpu.speed cpu.times#idle cpu.times#idle cpu.times#user - |> print_endline) diff --git a/examples_lwt/Makefile b/examples_lwt/Makefile deleted file mode 100644 index d4b5728..0000000 --- a/examples_lwt/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -pkgs := nodejs.high_level_lwt -js_debug := --debug-info --no-inline --pretty - -file_system:file_system.ml - ocamlfind ocamlc -g -package ${pkgs} -linkpkg $< -o $@ - js_of_ocaml ${js_debug} $@ -o $@.js - node $@.js - -clean: - @rm -f *.cmi *.cmo *.cmt a.out *.js \ - file_system - - -.PHONY:clean file_system diff --git a/examples_lwt/file_system.ml b/examples_lwt/file_system.ml deleted file mode 100644 index f6fdc40..0000000 --- a/examples_lwt/file_system.ml +++ /dev/null @@ -1,8 +0,0 @@ -open Lwt.Infix - -let () = - (Nodejs_high_level_lwt.Fs.read_file (Nodejs_high_level.__filename ()) - >>= fun (err, result) -> result#to_string |> print_endline |> Lwt.return - >|= fun () -> print_endline "Finished Program and Ordered Async") - |> Lwt.ignore_result - diff --git a/myocamlbuild.ml b/myocamlbuild.ml deleted file mode 100644 index 6fc4bc2..0000000 --- a/myocamlbuild.ml +++ /dev/null @@ -1,839 +0,0 @@ -(* OASIS_START *) -(* DO NOT EDIT (digest: d27d68596339d1cde6f19123586c3ac2) *) -module OASISGettext = struct -(* # 22 "src/oasis/OASISGettext.ml" *) - - - let ns_ str = - str - - - let s_ str = - str - - - let f_ (str: ('a, 'b, 'c, 'd) format4) = - str - - - let fn_ fmt1 fmt2 n = - if n = 1 then - fmt1^^"" - else - fmt2^^"" - - - let init = - [] - - -end - -module OASISString = struct -(* # 22 "src/oasis/OASISString.ml" *) - - - (** Various string utilities. - - Mostly inspired by extlib and batteries ExtString and BatString libraries. - - @author Sylvain Le Gall - *) - - - let nsplitf str f = - if str = "" then - [] - else - let buf = Buffer.create 13 in - let lst = ref [] in - let push () = - lst := Buffer.contents buf :: !lst; - Buffer.clear buf - in - let str_len = String.length str in - for i = 0 to str_len - 1 do - if f str.[i] then - push () - else - Buffer.add_char buf str.[i] - done; - push (); - List.rev !lst - - - (** [nsplit c s] Split the string [s] at char [c]. It doesn't include the - separator. - *) - let nsplit str c = - nsplitf str ((=) c) - - - let find ~what ?(offset=0) str = - let what_idx = ref 0 in - let str_idx = ref offset in - while !str_idx < String.length str && - !what_idx < String.length what do - if str.[!str_idx] = what.[!what_idx] then - incr what_idx - else - what_idx := 0; - incr str_idx - done; - if !what_idx <> String.length what then - raise Not_found - else - !str_idx - !what_idx - - - let sub_start str len = - let str_len = String.length str in - if len >= str_len then - "" - else - String.sub str len (str_len - len) - - - let sub_end ?(offset=0) str len = - let str_len = String.length str in - if len >= str_len then - "" - else - String.sub str 0 (str_len - len) - - - let starts_with ~what ?(offset=0) str = - let what_idx = ref 0 in - let str_idx = ref offset in - let ok = ref true in - while !ok && - !str_idx < String.length str && - !what_idx < String.length what do - if str.[!str_idx] = what.[!what_idx] then - incr what_idx - else - ok := false; - incr str_idx - done; - if !what_idx = String.length what then - true - else - false - - - let strip_starts_with ~what str = - if starts_with ~what str then - sub_start str (String.length what) - else - raise Not_found - - - let ends_with ~what ?(offset=0) str = - let what_idx = ref ((String.length what) - 1) in - let str_idx = ref ((String.length str) - 1) in - let ok = ref true in - while !ok && - offset <= !str_idx && - 0 <= !what_idx do - if str.[!str_idx] = what.[!what_idx] then - decr what_idx - else - ok := false; - decr str_idx - done; - if !what_idx = -1 then - true - else - false - - - let strip_ends_with ~what str = - if ends_with ~what str then - sub_end str (String.length what) - else - raise Not_found - - - let replace_chars f s = - let buf = Buffer.create (String.length s) in - String.iter (fun c -> Buffer.add_char buf (f c)) s; - Buffer.contents buf - - let lowercase_ascii = - replace_chars - (fun c -> - if (c >= 'A' && c <= 'Z') then - Char.chr (Char.code c + 32) - else - c) - - let uncapitalize_ascii s = - if s <> "" then - (lowercase_ascii (String.sub s 0 1)) ^ (String.sub s 1 ((String.length s) - 1)) - else - s - - let uppercase_ascii = - replace_chars - (fun c -> - if (c >= 'a' && c <= 'z') then - Char.chr (Char.code c - 32) - else - c) - - let capitalize_ascii s = - if s <> "" then - (uppercase_ascii (String.sub s 0 1)) ^ (String.sub s 1 ((String.length s) - 1)) - else - s - -end - -module OASISExpr = struct -(* # 22 "src/oasis/OASISExpr.ml" *) - - - - - - open OASISGettext - - - type test = string - - - type flag = string - - - type t = - | EBool of bool - | ENot of t - | EAnd of t * t - | EOr of t * t - | EFlag of flag - | ETest of test * string - - - - type 'a choices = (t * 'a) list - - - let eval var_get t = - let rec eval' = - function - | EBool b -> - b - - | ENot e -> - not (eval' e) - - | EAnd (e1, e2) -> - (eval' e1) && (eval' e2) - - | EOr (e1, e2) -> - (eval' e1) || (eval' e2) - - | EFlag nm -> - let v = - var_get nm - in - assert(v = "true" || v = "false"); - (v = "true") - - | ETest (nm, vl) -> - let v = - var_get nm - in - (v = vl) - in - eval' t - - - let choose ?printer ?name var_get lst = - let rec choose_aux = - function - | (cond, vl) :: tl -> - if eval var_get cond then - vl - else - choose_aux tl - | [] -> - let str_lst = - if lst = [] then - s_ "" - else - String.concat - (s_ ", ") - (List.map - (fun (cond, vl) -> - match printer with - | Some p -> p vl - | None -> s_ "") - lst) - in - match name with - | Some nm -> - failwith - (Printf.sprintf - (f_ "No result for the choice list '%s': %s") - nm str_lst) - | None -> - failwith - (Printf.sprintf - (f_ "No result for a choice list: %s") - str_lst) - in - choose_aux (List.rev lst) - - -end - - -# 292 "myocamlbuild.ml" -module BaseEnvLight = struct -(* # 22 "src/base/BaseEnvLight.ml" *) - - - module MapString = Map.Make(String) - - - type t = string MapString.t - - - let default_filename = - Filename.concat - (Sys.getcwd ()) - "setup.data" - - - let load ?(allow_empty=false) ?(filename=default_filename) () = - if Sys.file_exists filename then - begin - let chn = - open_in_bin filename - in - let st = - Stream.of_channel chn - in - let line = - ref 1 - in - let st_line = - Stream.from - (fun _ -> - try - match Stream.next st with - | '\n' -> incr line; Some '\n' - | c -> Some c - with Stream.Failure -> None) - in - let lexer = - Genlex.make_lexer ["="] st_line - in - let rec read_file mp = - match Stream.npeek 3 lexer with - | [Genlex.Ident nm; Genlex.Kwd "="; Genlex.String value] -> - Stream.junk lexer; - Stream.junk lexer; - Stream.junk lexer; - read_file (MapString.add nm value mp) - | [] -> - mp - | _ -> - failwith - (Printf.sprintf - "Malformed data file '%s' line %d" - filename !line) - in - let mp = - read_file MapString.empty - in - close_in chn; - mp - end - else if allow_empty then - begin - MapString.empty - end - else - begin - failwith - (Printf.sprintf - "Unable to load environment, the file '%s' doesn't exist." - filename) - end - - - let rec var_expand str env = - let buff = - Buffer.create ((String.length str) * 2) - in - Buffer.add_substitute - buff - (fun var -> - try - var_expand (MapString.find var env) env - with Not_found -> - failwith - (Printf.sprintf - "No variable %s defined when trying to expand %S." - var - str)) - str; - Buffer.contents buff - - - let var_get name env = - var_expand (MapString.find name env) env - - - let var_choose lst env = - OASISExpr.choose - (fun nm -> var_get nm env) - lst -end - - -# 397 "myocamlbuild.ml" -module MyOCamlbuildFindlib = struct -(* # 22 "src/plugins/ocamlbuild/MyOCamlbuildFindlib.ml" *) - - - (** OCamlbuild extension, copied from - * http://brion.inria.fr/gallium/index.php/Using_ocamlfind_with_ocamlbuild - * by N. Pouillard and others - * - * Updated on 2009/02/28 - * - * Modified by Sylvain Le Gall - *) - open Ocamlbuild_plugin - - type conf = - { no_automatic_syntax: bool; - } - - (* these functions are not really officially exported *) - let run_and_read = - Ocamlbuild_pack.My_unix.run_and_read - - - let blank_sep_strings = - Ocamlbuild_pack.Lexers.blank_sep_strings - - - let exec_from_conf exec = - let exec = - let env_filename = Pathname.basename BaseEnvLight.default_filename in - let env = BaseEnvLight.load ~filename:env_filename ~allow_empty:true () in - try - BaseEnvLight.var_get exec env - with Not_found -> - Printf.eprintf "W: Cannot get variable %s\n" exec; - exec - in - let fix_win32 str = - if Sys.os_type = "Win32" then begin - let buff = Buffer.create (String.length str) in - (* Adapt for windowsi, ocamlbuild + win32 has a hard time to handle '\\'. - *) - String.iter - (fun c -> Buffer.add_char buff (if c = '\\' then '/' else c)) - str; - Buffer.contents buff - end else begin - str - end - in - fix_win32 exec - - let split s ch = - let buf = Buffer.create 13 in - let x = ref [] in - let flush () = - x := (Buffer.contents buf) :: !x; - Buffer.clear buf - in - String.iter - (fun c -> - if c = ch then - flush () - else - Buffer.add_char buf c) - s; - flush (); - List.rev !x - - - let split_nl s = split s '\n' - - - let before_space s = - try - String.before s (String.index s ' ') - with Not_found -> s - - (* ocamlfind command *) - let ocamlfind x = S[Sh (exec_from_conf "ocamlfind"); x] - - (* This lists all supported packages. *) - let find_packages () = - List.map before_space (split_nl & run_and_read (exec_from_conf "ocamlfind" ^ " list")) - - - (* Mock to list available syntaxes. *) - let find_syntaxes () = ["camlp4o"; "camlp4r"] - - - let well_known_syntax = [ - "camlp4.quotations.o"; - "camlp4.quotations.r"; - "camlp4.exceptiontracer"; - "camlp4.extend"; - "camlp4.foldgenerator"; - "camlp4.listcomprehension"; - "camlp4.locationstripper"; - "camlp4.macro"; - "camlp4.mapgenerator"; - "camlp4.metagenerator"; - "camlp4.profiler"; - "camlp4.tracer" - ] - - - let dispatch conf = - function - | After_options -> - (* By using Before_options one let command line options have an higher - * priority on the contrary using After_options will guarantee to have - * the higher priority override default commands by ocamlfind ones *) - Options.ocamlc := ocamlfind & A"ocamlc"; - Options.ocamlopt := ocamlfind & A"ocamlopt"; - Options.ocamldep := ocamlfind & A"ocamldep"; - Options.ocamldoc := ocamlfind & A"ocamldoc"; - Options.ocamlmktop := ocamlfind & A"ocamlmktop"; - Options.ocamlmklib := ocamlfind & A"ocamlmklib" - - | After_rules -> - - (* When one link an OCaml library/binary/package, one should use - * -linkpkg *) - flag ["ocaml"; "link"; "program"] & A"-linkpkg"; - - if not (conf.no_automatic_syntax) then begin - (* For each ocamlfind package one inject the -package option when - * compiling, computing dependencies, generating documentation and - * linking. *) - List.iter - begin fun pkg -> - let base_args = [A"-package"; A pkg] in - (* TODO: consider how to really choose camlp4o or camlp4r. *) - let syn_args = [A"-syntax"; A "camlp4o"] in - let (args, pargs) = - (* Heuristic to identify syntax extensions: whether they end in - ".syntax"; some might not. - *) - if Filename.check_suffix pkg "syntax" || - List.mem pkg well_known_syntax then - (syn_args @ base_args, syn_args) - else - (base_args, []) - in - flag ["ocaml"; "compile"; "pkg_"^pkg] & S args; - flag ["ocaml"; "ocamldep"; "pkg_"^pkg] & S args; - flag ["ocaml"; "doc"; "pkg_"^pkg] & S args; - flag ["ocaml"; "link"; "pkg_"^pkg] & S base_args; - flag ["ocaml"; "infer_interface"; "pkg_"^pkg] & S args; - - (* TODO: Check if this is allowed for OCaml < 3.12.1 *) - flag ["ocaml"; "compile"; "package("^pkg^")"] & S pargs; - flag ["ocaml"; "ocamldep"; "package("^pkg^")"] & S pargs; - flag ["ocaml"; "doc"; "package("^pkg^")"] & S pargs; - flag ["ocaml"; "infer_interface"; "package("^pkg^")"] & S pargs; - end - (find_packages ()); - end; - - (* Like -package but for extensions syntax. Morover -syntax is useless - * when linking. *) - List.iter begin fun syntax -> - flag ["ocaml"; "compile"; "syntax_"^syntax] & S[A"-syntax"; A syntax]; - flag ["ocaml"; "ocamldep"; "syntax_"^syntax] & S[A"-syntax"; A syntax]; - flag ["ocaml"; "doc"; "syntax_"^syntax] & S[A"-syntax"; A syntax]; - flag ["ocaml"; "infer_interface"; "syntax_"^syntax] & - S[A"-syntax"; A syntax]; - end (find_syntaxes ()); - - (* The default "thread" tag is not compatible with ocamlfind. - * Indeed, the default rules add the "threads.cma" or "threads.cmxa" - * options when using this tag. When using the "-linkpkg" option with - * ocamlfind, this module will then be added twice on the command line. - * - * To solve this, one approach is to add the "-thread" option when using - * the "threads" package using the previous plugin. - *) - flag ["ocaml"; "pkg_threads"; "compile"] (S[A "-thread"]); - flag ["ocaml"; "pkg_threads"; "doc"] (S[A "-I"; A "+threads"]); - flag ["ocaml"; "pkg_threads"; "link"] (S[A "-thread"]); - flag ["ocaml"; "pkg_threads"; "infer_interface"] (S[A "-thread"]); - flag ["ocaml"; "package(threads)"; "compile"] (S[A "-thread"]); - flag ["ocaml"; "package(threads)"; "doc"] (S[A "-I"; A "+threads"]); - flag ["ocaml"; "package(threads)"; "link"] (S[A "-thread"]); - flag ["ocaml"; "package(threads)"; "infer_interface"] (S[A "-thread"]); - - | _ -> - () -end - -module MyOCamlbuildBase = struct -(* # 22 "src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *) - - - (** Base functions for writing myocamlbuild.ml - @author Sylvain Le Gall - *) - - - - - - open Ocamlbuild_plugin - module OC = Ocamlbuild_pack.Ocaml_compiler - - - type dir = string - type file = string - type name = string - type tag = string - - -(* # 62 "src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *) - - - type t = - { - lib_ocaml: (name * dir list * string list) list; - lib_c: (name * dir * file list) list; - flags: (tag list * (spec OASISExpr.choices)) list; - (* Replace the 'dir: include' from _tags by a precise interdepends in - * directory. - *) - includes: (dir * dir list) list; - } - - - let env_filename = - Pathname.basename - BaseEnvLight.default_filename - - - let dispatch_combine lst = - fun e -> - List.iter - (fun dispatch -> dispatch e) - lst - - - let tag_libstubs nm = - "use_lib"^nm^"_stubs" - - - let nm_libstubs nm = - nm^"_stubs" - - - let dispatch t e = - let env = - BaseEnvLight.load - ~filename:env_filename - ~allow_empty:true - () - in - match e with - | Before_options -> - let no_trailing_dot s = - if String.length s >= 1 && s.[0] = '.' then - String.sub s 1 ((String.length s) - 1) - else - s - in - List.iter - (fun (opt, var) -> - try - opt := no_trailing_dot (BaseEnvLight.var_get var env) - with Not_found -> - Printf.eprintf "W: Cannot get variable %s\n" var) - [ - Options.ext_obj, "ext_obj"; - Options.ext_lib, "ext_lib"; - Options.ext_dll, "ext_dll"; - ] - - | After_rules -> - (* Declare OCaml libraries *) - List.iter - (function - | nm, [], intf_modules -> - ocaml_lib nm; - let cmis = - List.map (fun m -> (OASISString.uncapitalize_ascii m) ^ ".cmi") - intf_modules in - dep ["ocaml"; "link"; "library"; "file:"^nm^".cma"] cmis - | nm, dir :: tl, intf_modules -> - ocaml_lib ~dir:dir (dir^"/"^nm); - List.iter - (fun dir -> - List.iter - (fun str -> - flag ["ocaml"; "use_"^nm; str] (S[A"-I"; P dir])) - ["compile"; "infer_interface"; "doc"]) - tl; - let cmis = - List.map (fun m -> dir^"/"^(OASISString.uncapitalize_ascii m)^".cmi") - intf_modules in - dep ["ocaml"; "link"; "library"; "file:"^dir^"/"^nm^".cma"] - cmis) - t.lib_ocaml; - - (* Declare directories dependencies, replace "include" in _tags. *) - List.iter - (fun (dir, include_dirs) -> - Pathname.define_context dir include_dirs) - t.includes; - - (* Declare C libraries *) - List.iter - (fun (lib, dir, headers) -> - (* Handle C part of library *) - flag ["link"; "library"; "ocaml"; "byte"; tag_libstubs lib] - (S[A"-dllib"; A("-l"^(nm_libstubs lib)); A"-cclib"; - A("-l"^(nm_libstubs lib))]); - - flag ["link"; "library"; "ocaml"; "native"; tag_libstubs lib] - (S[A"-cclib"; A("-l"^(nm_libstubs lib))]); - - flag ["link"; "program"; "ocaml"; "byte"; tag_libstubs lib] - (S[A"-dllib"; A("dll"^(nm_libstubs lib))]); - - (* When ocaml link something that use the C library, then one - need that file to be up to date. - This holds both for programs and for libraries. - *) - dep ["link"; "ocaml"; tag_libstubs lib] - [dir/"lib"^(nm_libstubs lib)^"."^(!Options.ext_lib)]; - - dep ["compile"; "ocaml"; tag_libstubs lib] - [dir/"lib"^(nm_libstubs lib)^"."^(!Options.ext_lib)]; - - (* TODO: be more specific about what depends on headers *) - (* Depends on .h files *) - dep ["compile"; "c"] - headers; - - (* Setup search path for lib *) - flag ["link"; "ocaml"; "use_"^lib] - (S[A"-I"; P(dir)]); - ) - t.lib_c; - - (* Add flags *) - List.iter - (fun (tags, cond_specs) -> - let spec = BaseEnvLight.var_choose cond_specs env in - let rec eval_specs = - function - | S lst -> S (List.map eval_specs lst) - | A str -> A (BaseEnvLight.var_expand str env) - | spec -> spec - in - flag tags & (eval_specs spec)) - t.flags - | _ -> - () - - - let dispatch_default conf t = - dispatch_combine - [ - dispatch t; - MyOCamlbuildFindlib.dispatch conf; - ] - - -end - - -# 766 "myocamlbuild.ml" -open Ocamlbuild_plugin;; -let package_default = - { - MyOCamlbuildBase.lib_ocaml = - [ - ("nodejs", ["src"], []); - ("high_level", ["src/higher_level"], []); - ("high_level_lwt", ["src/higher_level_lwt"], []) - ]; - lib_c = []; - flags = - [ - (["oasis_library_nodejs_byte"; "ocaml"; "link"; "byte"], - [ - (OASISExpr.EBool true, - S [A "-g"; A "-w"; A "+a-4-40..42-44-45-48"]) - ]); - (["oasis_library_nodejs_byte"; "ocaml"; "ocamldep"; "byte"], - [ - (OASISExpr.EBool true, - S [A "-g"; A "-w"; A "+a-4-40..42-44-45-48"]) - ]); - (["oasis_library_nodejs_byte"; "ocaml"; "compile"; "byte"], - [ - (OASISExpr.EBool true, - S [A "-g"; A "-w"; A "+a-4-40..42-44-45-48"]) - ]); - (["oasis_library_high_level_byte"; "ocaml"; "link"; "byte"], - [ - (OASISExpr.EBool true, - S [A "-g"; A "-w"; A "+a-4-40..42-44-45-48"]) - ]); - (["oasis_library_high_level_byte"; "ocaml"; "ocamldep"; "byte"], - [ - (OASISExpr.EBool true, - S [A "-g"; A "-w"; A "+a-4-40..42-44-45-48"]) - ]); - (["oasis_library_high_level_byte"; "ocaml"; "compile"; "byte"], - [ - (OASISExpr.EBool true, - S [A "-g"; A "-w"; A "+a-4-40..42-44-45-48"]) - ]); - (["oasis_library_high_level_lwt_byte"; "ocaml"; "link"; "byte"], - [ - (OASISExpr.EBool true, - S [A "-g"; A "-w"; A "+a-4-40..42-44-45-48"]) - ]); - (["oasis_library_high_level_lwt_byte"; "ocaml"; "ocamldep"; "byte"], - [ - (OASISExpr.EBool true, - S [A "-g"; A "-w"; A "+a-4-40..42-44-45-48"]) - ]); - (["oasis_library_high_level_lwt_byte"; "ocaml"; "compile"; "byte"], - [ - (OASISExpr.EBool true, - S [A "-g"; A "-w"; A "+a-4-40..42-44-45-48"]) - ]) - ]; - includes = - [ - ("src/higher_level_lwt", ["src/higher_level"]); - ("src/higher_level", ["src"]) - ] - } - ;; - -let conf = {MyOCamlbuildFindlib.no_automatic_syntax = false} - -let dispatch_default = MyOCamlbuildBase.dispatch_default conf package_default;; - -# 838 "myocamlbuild.ml" -(* OASIS_STOP *) -Ocamlbuild_plugin.dispatch dispatch_default;; diff --git a/nodejs.opam b/nodejs.opam new file mode 100644 index 0000000..be3d465 --- /dev/null +++ b/nodejs.opam @@ -0,0 +1,33 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +synopsis: "js_of_ocaml bindings for nodejs" +description: """ +Write OCaml, run on node; these are js_of_ocaml bindings to the node +JavaScript API. Get all the power of the node ecosystem with the type +safety of OCaml.""" +maintainer: ["Edgar Aroutiounnian "] +authors: ["Edgar Aroutiounnian "] +license: "BSD-3-clause" +homepage: "https://github.com/fxfactorial/ocaml-nodejs" +bug-reports: "https://github.com/fxfactorial/ocaml-nodejs/issues" +depends: [ + "dune" {>= "3.0"} + "ocaml" + "js_of_ocaml" {>= "2.5.2"} + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://github.com/fxfactorial/ocaml-nodejs.git" diff --git a/opam/descr b/opam/descr deleted file mode 100644 index a8f0434..0000000 --- a/opam/descr +++ /dev/null @@ -1,14 +0,0 @@ -js_of_ocaml bindings for nodejs - -Write OCaml, run on node; these are js_of_ocaml bindings to the node -JavaScript API. Get all the power of the node ecosystem with the type -safety of OCaml. - -Here's how easy it is to make a server. - - -Compile, run with: - -$ ocamlfind ocamlc c.ml -linkpkg -package nodejs.high_level -o T.out -$ js_of_ocaml T.out -$ node T.js \ No newline at end of file diff --git a/opam/findlib b/opam/findlib deleted file mode 100644 index e36de65..0000000 --- a/opam/findlib +++ /dev/null @@ -1 +0,0 @@ -nodejs diff --git a/opam/opam b/opam/opam deleted file mode 100644 index c913675..0000000 --- a/opam/opam +++ /dev/null @@ -1,46 +0,0 @@ -# -*- conf -*- -opam-version: "1.2" -name: "nodejs" -version: "1.0.0" -maintainer: "Edgar Aroutiounian " -authors: [ "Edgar Aroutiounian " ] -license: "BSD-3-clause" -homepage: "https://github.com/fxfactorial/ocaml-nodejs" -dev-repo: "https://github.com/fxfactorial/ocaml-nodejs.git" -bug-reports: "https://github.com/fxfactorial/ocaml-nodejs/issues" -build: [ - ["ocaml" "setup.ml" "-configure" "--prefix" prefix] - ["ocaml" "setup.ml" "-build"] -] -install: ["ocaml" "setup.ml" "-install"] -remove: [ - ["ocamlfind" "remove" "nodejs"] -] -build-test: [ - ["ocaml" "setup.ml" "-configure" "--enable-tests"] - ["ocaml" "setup.ml" "-build"] - ["ocaml" "setup.ml" "-test"] -] -depends: [ - "js_of_ocaml" {>= "2.8.1"} - "lwt" {>= "2.5.2"} - "ocamlfind" {build} - "oasis" -] -available: [ ocaml-version >= "4.02.3" ] - -depexts: [ - [["debian"] ["nodejs"]] - [["homebrew" "osx"] ["node"]] - [["ubuntu"] ["nodejs"]] -] - -post-messages: [ - "Now you can write OCaml and execute on node." - "Example assuming file name of c.ml:" - " - " - "ocamlfind ocamlc c.ml -linkpkg -package nodejs -o T.out" - "js_of_ocaml T.out" - "node T.js" -] diff --git a/setup.ml b/setup.ml deleted file mode 100644 index 0345fe1..0000000 --- a/setup.ml +++ /dev/null @@ -1,37 +0,0 @@ -(* setup.ml generated for the first time by OASIS v0.4.5 *) - -(* OASIS_START *) -(* DO NOT EDIT (digest: 9852805d5c19ca1cb6abefde2dcea323) *) -(******************************************************************************) -(* OASIS: architecture for building OCaml libraries and applications *) -(* *) -(* Copyright (C) 2011-2013, Sylvain Le Gall *) -(* Copyright (C) 2008-2011, OCamlCore SARL *) -(* *) -(* This library is free software; you can redistribute it and/or modify it *) -(* under the terms of the GNU Lesser General Public License as published by *) -(* the Free Software Foundation; either version 2.1 of the License, or (at *) -(* your option) any later version, with the OCaml static compilation *) -(* exception. *) -(* *) -(* This library is distributed in the hope that it will be useful, but *) -(* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *) -(* or FITNESS FOR A PARTICULAR PURPOSE. See the file COPYING for more *) -(* details. *) -(* *) -(* You should have received a copy of the GNU Lesser General Public License *) -(* along with this library; if not, write to the Free Software Foundation, *) -(* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *) -(******************************************************************************) - -let () = - try - Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") - with Not_found -> () -;; -#use "topfind";; -#require "oasis.dynrun";; -open OASISDynRun;; - -(* OASIS_STOP *) -let () = setup ();; diff --git a/src/META b/src/META deleted file mode 100644 index 19ccf84..0000000 --- a/src/META +++ /dev/null @@ -1,27 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 6a9fa993d66c7c4cc0616548c2524e17) -version = "1.0.0" -description = "js_of_ocaml bindings for nodejs" -requires = "js_of_ocaml js_of_ocaml.ppx lwt.ppx lwt" -archive(byte) = "nodejs.cma" -archive(byte, plugin) = "nodejs.cma" -exists_if = "nodejs.cma" -package "high_level_lwt" ( - version = "1.0.0" - description = "js_of_ocaml bindings for nodejs" - requires = "nodejs.high_level" - archive(byte) = "high_level_lwt.cma" - archive(byte, plugin) = "high_level_lwt.cma" - exists_if = "high_level_lwt.cma" -) - -package "high_level" ( - version = "1.0.0" - description = "js_of_ocaml bindings for nodejs" - requires = "nodejs" - archive(byte) = "high_level.cma" - archive(byte, plugin) = "high_level.cma" - exists_if = "high_level.cma" -) -# OASIS_STOP - diff --git a/src/dune b/src/dune new file mode 100644 index 0000000..dacdbd2 --- /dev/null +++ b/src/dune @@ -0,0 +1,18 @@ +(library + (public_name nodejs) + (modules nodejs) + (libraries js_of_ocaml) + (preprocess + (pps js_of_ocaml-ppx))) + +(library + (name nodejs_high_level) + (modules nodejs_high_level) + (libraries js_of_ocaml nodejs) + (preprocess + (pps js_of_ocaml-ppx))) + +(library + (name nodejs_high_level_lwt) + (modules nodejs_high_level_lwt) + (libraries lwt nodejs nodejs_high_level)) diff --git a/src/higher_level/high_level.mldylib b/src/higher_level/high_level.mldylib deleted file mode 100644 index 2ab2aed..0000000 --- a/src/higher_level/high_level.mldylib +++ /dev/null @@ -1,4 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 1e86897f6195533f4aa156440679fd4e) -Nodejs_high_level -# OASIS_STOP diff --git a/src/higher_level/high_level.mllib b/src/higher_level/high_level.mllib deleted file mode 100644 index 2ab2aed..0000000 --- a/src/higher_level/high_level.mllib +++ /dev/null @@ -1,4 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 1e86897f6195533f4aa156440679fd4e) -Nodejs_high_level -# OASIS_STOP diff --git a/src/higher_level/higher_level.mldylib b/src/higher_level/higher_level.mldylib deleted file mode 100644 index 2ab2aed..0000000 --- a/src/higher_level/higher_level.mldylib +++ /dev/null @@ -1,4 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 1e86897f6195533f4aa156440679fd4e) -Nodejs_high_level -# OASIS_STOP diff --git a/src/higher_level/higher_level.mllib b/src/higher_level/higher_level.mllib deleted file mode 100644 index 2ab2aed..0000000 --- a/src/higher_level/higher_level.mllib +++ /dev/null @@ -1,4 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 1e86897f6195533f4aa156440679fd4e) -Nodejs_high_level -# OASIS_STOP diff --git a/src/higher_level/nodejs_high_level.ml b/src/higher_level/nodejs_high_level.ml deleted file mode 100644 index 9bb07a0..0000000 --- a/src/higher_level/nodejs_high_level.ml +++ /dev/null @@ -1,201 +0,0 @@ -let __filename () = Nodejs.__filename () |> Js.to_string - -let __dirname () = Nodejs.__dirname () |> Js.to_string - - -module Buffer = struct - - type exn += Buffer_not_init - - class buffer ?(default=1024) fresh_buffer = object - - val mutable buffer_handle : Nodejs.Buffer.buffer Js.t Js.opt = Js.null - val mutable init_buffer = false - - initializer - if fresh_buffer - then buffer_handle <- - Nodejs.Buffer.buffer_static##alloc default |> Js.Opt.return; - init_buffer <- true - - method set_handle new_handle = - buffer_handle <- new_handle |> Js.Opt.return; - init_buffer <- true - - method to_string = - if init_buffer then begin - let b = Js.Opt.get buffer_handle (fun _ -> assert false) in - b##toString |> Js.to_string - end else raise Buffer_not_init - - end - -end - - -module Events = struct - - class event_emmiter = object - val k = new%js Nodejs.Events.event_emitter - method event_names = - k##eventNames - |> Js.str_array - |> Js.to_array - |> Array.map Js.to_string - |> Array.to_list - - method add_listener : 'a 'b. string -> ('a -> 'b) -> unit = fun event_name callback -> - k##addListener (Js.string event_name) (Js.wrap_callback callback) - - method emit event_name = k##emit (Js.string event_name) - - end - -end - -module Fs = struct - - type flag = Read | Write | Read_write - - let string_of_flag = function Read -> "r" | Write -> "w" | Read_write -> "rw" - - let read_file_sync file_name file_flag = - let result = - Nodejs.Fs.fs##readFileSync - (Js.string file_name) - (object%js - val encoding = Js.null - val flag = Js.string (string_of_flag file_flag) - end) - in - result##toString |> Js.to_string - - let read_file_async file_name callback = - let wrapped = fun err given_buffer -> - let b = new Buffer.buffer false in - b#set_handle given_buffer; - callback err b - in - Nodejs.Fs.fs##readFile (Js.string file_name) (Js.wrap_callback wrapped) - -end - -module Net = struct - - type exn += Socket_not_init - - class socket fresh_socket = object - - val mutable socket_handle : Nodejs.Net.socket Js.t Js.opt = Js.null - val mutable init_socket = false - - initializer - if fresh_socket - then socket_handle <- new%js Nodejs.Net.socket |> Js.Opt.return; - init_socket <- true - - method set_handle raw_socket = - socket_handle <- raw_socket |> Js.Opt.return; - init_socket <- true - - method write data = - if init_socket then begin - let c = Js.Opt.get socket_handle (fun _ -> assert false) in - c##write (Nodejs.Buffer.buffer_static##from (Js.string data)) - end else raise Socket_not_init - - end - - - class server listener = object - val k = - let wrapped = fun given_socket -> - let s = new socket false in - s#set_handle given_socket; - listener s - in - Nodejs.Net.net##createServer_withConnListener (Js.wrap_callback wrapped) - (* method listen : 'a 'b. int -> ('a -> 'b) -> unit = fun port callback -> *) - (* k##listen port (Js.wrap_callback callback) *) - method listen port callback = k##listen port (Js.wrap_callback callback) - - end - - (* let create_server listener = *) - (* Nodejs.Net.net##createServer_withConnListener (Js.wrap_callback listener) *) -end - -module Os = struct - - (* Later hide with mli *) - let os_handle = Nodejs.Os.os - - type cpu_stat = { model: string; - speed: int; - times: } - - type endianess = Big | Little - - type platform = Aix | Darwin | Freebsd | Linux | Openbsd | Sunos | Win32 | Android - - (* Hide as well in mli *) - let endianess_of_string = - function "BE" -> Big | "LE" -> Little | _ -> assert false - - let platform_of_string = function - | "aix" -> Aix - | "darwin" -> Darwin - | "freebsd" -> Freebsd - | "linux" -> Linux - (* TODO FINISH*) - | _ -> assert false - - let cpu_stats () = - os_handle##cpus - |> Js.to_array - |> Array.map (fun c -> - {model = c##.model |> Js.to_string; - speed = c##.speed; - times = object - method user = c##.times##.user - method nice = c##.times##.nice - method sys = c##.times##.sys - method idle = c##.times##.idle - method irq = c##.times##.irq - end} - ) - |> Array.to_list - - let endianess () = - os_handle##endianness |> Js.to_string |> endianess_of_string - - let free_memory_available () = os_handle##freemem - - let home_directory () = os_handle##homedir |> Js.to_string - - let hostname () = os_handle##hostname |> Js.to_string - - let load_average () = os_handle##loadavg |> Js.to_array - -end - -class process = object - val js = Nodejs.process - method arguments = - js##.argv |> Js.to_array |> Array.map Js.to_string |> Array.to_list - method abort = js##abort - method arch = js##.arch |> Js.to_string - method change_dir s = js##chdir (Js.string s) - method config : 'a. 'a Js.t = js##.config - method connect = js##.connected |> Js.to_bool - method current_working_directory = js##cwd |> Js.to_string - method environment_variables : 'b. 'b Js.t = js##.env - method pid = js##.pid - method exit e = js##exit e -end - -let process = new process diff --git a/src/higher_level_lwt/high_level_lwt.mldylib b/src/higher_level_lwt/high_level_lwt.mldylib deleted file mode 100644 index 57a9651..0000000 --- a/src/higher_level_lwt/high_level_lwt.mldylib +++ /dev/null @@ -1,4 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 8799187c1c9c2dd0ce6ec0d2748aa58b) -Nodejs_high_level_lwt -# OASIS_STOP diff --git a/src/higher_level_lwt/high_level_lwt.mllib b/src/higher_level_lwt/high_level_lwt.mllib deleted file mode 100644 index 57a9651..0000000 --- a/src/higher_level_lwt/high_level_lwt.mllib +++ /dev/null @@ -1,4 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 8799187c1c9c2dd0ce6ec0d2748aa58b) -Nodejs_high_level_lwt -# OASIS_STOP diff --git a/src/higher_level_lwt/nodejs_high_level_lwt.ml b/src/higher_level_lwt/nodejs_high_level_lwt.ml deleted file mode 100644 index ae5383e..0000000 --- a/src/higher_level_lwt/nodejs_high_level_lwt.ml +++ /dev/null @@ -1,12 +0,0 @@ - -module Fs = struct - - let read_file filename = - let t, w = Lwt.task () in - Nodejs_high_level.Fs.read_file_async - filename - (fun err content -> Lwt.wakeup w (err, content)); - t - -end - diff --git a/src/nodejs.ml b/src/nodejs.ml index 0d23ac3..b4875c9 100644 --- a/src/nodejs.ml +++ b/src/nodejs.ml @@ -1,13 +1,15 @@ +open Js_of_ocaml + (** Raw call for doing require("some_module") *) module Bindings_utils = struct - let require_module s = Js.Unsafe.fun_call - (Js.Unsafe.js_expr "require") - [|Js.Unsafe.inject (Js.string s)|] + (Js.Unsafe.pure_js_expr "require") + [| Js.Unsafe.inject (Js.string s) |] let ( !@ ) f = Js.wrap_callback f + let ( !! ) o = Js.Unsafe.inject o (** Get the field of a JavaScript Object *) @@ -19,8 +21,8 @@ module Bindings_utils = struct (** Call method of a JavaScript object *) let m = Js.Unsafe.meth_call - (** Inject something as a JS object, be sure its Js.t already, - functions seemingly exempt *) + (** Inject something as a JS object, be sure its Js.t already, functions + seemingly exempt *) let i = Js.Unsafe.inject (** Turn a JavaScript Object into a string *) @@ -42,212 +44,270 @@ module Bindings_utils = struct g |> Js.str_array |> Js.to_array |> Array.map Js.to_string |> Array.to_list (** Turn OCaml list of strings into JavaScript string array *) - let of_string_list g = - g |> Array.of_list |> Array.map Js.string |> Js.array + let of_string_list g = g |> Array.of_list |> Array.map Js.string |> Js.array (** Get all keys of an Object *) let keys obj = - m (Js.Unsafe.variable "Object") "keys" [|obj|] + m (Js.Unsafe.pure_js_expr "Object") "keys" [| obj |] |> Js.to_array |> Array.map Js.to_string |> Array.to_list - (** Call a function for each value of each key in an Object, throw - away result *) + (** Call a function for each value of each key in an Object, throw away result *) let for_each_iter ~f obj = keys obj |> List.iter (fun k -> f (Js.Unsafe.get obj k)) - (** Call a function for each value of each key in an Object, keep - result *) + (** Call a function for each value of each key in an Object, keep result *) let for_each_map ~f obj = keys obj |> List.map (fun k -> f k (Js.Unsafe.get obj k)) let debug thing field = - Firebug.console##log (m (thing field) "toString" [||]); - + Firebug.console##log (m (thing field) "toString" [||]) end type error_arg = Js.error Js.opt -let __filename () = (Js.Unsafe.eval_string "__filename" : Js.js_string Js.t) +let __filename () : Js.js_string Js.t = Js.Unsafe.eval_string "__filename" -let __dirname () = (Js.Unsafe.eval_string "__dirname" : Js.js_string Js.t) +let __dirname () : Js.js_string Js.t = Js.Unsafe.eval_string "__dirname" module Events = struct + class type event_emitter = + object ('self) + (* method on : Js.js_string Js.t -> 'a Js.callback -> unit Js.meth *) + method on_withNoArgs : + Js.js_string Js.t -> (unit -> unit) Js.callback -> unit Js.meth - class type event_emitter = object('self) - (* method on : Js.js_string Js.t -> 'a Js.callback -> unit Js.meth *) - method on_withNoArgs : - Js.js_string Js.t -> - (unit -> unit) Js.callback -> - unit Js.meth - method on_WithOneArg : - Js.js_string Js.t -> - (Js.Unsafe.any Js.t -> unit) Js.callback -> - unit Js.meth - method on_WithTwoArg : - Js.js_string Js.t -> - (Js.Unsafe.any Js.t -> Js.Unsafe.any Js.t -> unit) Js.callback -> - unit Js.meth - method emit : Js.js_string Js.t -> unit Js.meth - method emit_withArg : Js.js_string Js.t -> 'a Js.t -> unit Js.meth - method emit_withTwoArgs : - Js.js_string Js.t -> 'a Js.t -> 'b Js.t -> unit Js.meth - method getMaxListeners : int Js.meth - - method once : Js.js_string Js.t -> 'a Js.callback -> unit Js.meth - - method addListener : Js.js_string Js.t -> 'a Js.callback -> unit Js.meth - - method eventNames : Js.string_array Js.t Js.meth + method on_WithOneArg : + Js.js_string Js.t + -> (Js.Unsafe.any Js.t -> unit) Js.callback + -> unit Js.meth - end + method on_WithTwoArg : + Js.js_string Js.t + -> (Js.Unsafe.any Js.t -> Js.Unsafe.any Js.t -> unit) Js.callback + -> unit Js.meth + + method emit : Js.js_string Js.t -> unit Js.meth + + method emit_withArg : Js.js_string Js.t -> 'a Js.t -> unit Js.meth + + method emit_withTwoArgs : + Js.js_string Js.t -> 'a Js.t -> 'b Js.t -> unit Js.meth + + method getMaxListeners : int Js.meth + + method once : Js.js_string Js.t -> 'a Js.callback -> unit Js.meth + + method addListener : Js.js_string Js.t -> 'a Js.callback -> unit Js.meth + + method eventNames : Js.string_array Js.t Js.meth + end let event_emitter : event_emitter Js.t Js.constr = Bindings_utils.require_module "events" - end module Buffer = struct + class type buffer = + object + method length : int Js.readonly_prop - class type buffer = object - method length : int Js.readonly_prop - method toString : Js.js_string Js.t Js.meth - method toJSON : 'a. 'a Js.t Js.meth - method swap16 : buffer Js.t Js.meth - method swap32 : buffer Js.t Js.meth + method toString : Js.js_string Js.t Js.meth - end + method toJSON : 'a. 'a Js.t Js.meth - class type buffer_static = object - method from_array : Typed_array.uint16Array -> buffer Js.t Js.meth - method from : Js.js_string Js.t -> buffer Js.t Js.meth - method from_withEncoding : - Js.js_string Js.t -> Js.js_string Js.t -> buffer Js.t Js.meth - method alloc : int -> buffer Js.t Js.meth - method compare : buffer Js.t -> buffer Js.t -> bool Js.t Js.meth - method isBuffer : 'a Js.t -> bool Js.t Js.meth - method isEncoding : Js.js_string Js.t -> bool Js.t Js.meth - end + method swap16 : buffer Js.t Js.meth - let buffer_static : buffer_static Js.t = Js.Unsafe.pure_js_expr "Buffer" + method swap32 : buffer Js.t Js.meth + end + + class type buffer_static = + object + method from_array : Typed_array.uint16Array -> buffer Js.t Js.meth + + method from : Js.js_string Js.t -> buffer Js.t Js.meth + + method from_withEncoding : + Js.js_string Js.t -> Js.js_string Js.t -> buffer Js.t Js.meth + + method alloc : int -> buffer Js.t Js.meth + + method compare : buffer Js.t -> buffer Js.t -> bool Js.t Js.meth + method isBuffer : 'a Js.t -> bool Js.t Js.meth + + method isEncoding : Js.js_string Js.t -> bool Js.t Js.meth + end + + let buffer_static : buffer_static Js.t = Js.Unsafe.pure_js_expr "Buffer" end module Stream = struct + class type stream = + object ('self) + inherit Events.event_emitter + end + + class type writable_stream = + object ('self) + inherit stream + (* method cork *) + (* method end_ *) + (* method setDefaultEncoding *) + (* method uncork *) + (* method write *) + end - class type stream = object('self) - inherit Events.event_emitter - end + class type readable_stream = + object ('self) + inherit stream - class type writable_stream = object('self) - inherit stream - (* method cork *) - (* method end_ *) - (* method setDefaultEncoding *) - (* method uncork *) - (* method write *) - end + method isPaused : bool Js.t Js.meth - class type readable_stream = object('self) - inherit stream - method isPaused : bool Js.t Js.meth - method pause : readable_stream Js.t Js.meth - method pipe : 'self Js.t -> writable_stream Js.t Js.meth - - (* method pipe_with_options : *) - (* writable_stream Js.t -> *) - (* #stream Js.t Js.meth *) - - method read : 'a Js.t Js.opt Js.meth - method read_withAmount : int -> 'a Js.t Js.opt Js.meth - method resume : readable_stream Js.t Js.meth - method setEncoding : Js.js_string Js.t -> readable_stream Js.t Js.meth - method unpipe : unit Js.meth - method unpipe_withDest : writable_stream Js.t -> unit Js.meth - method unshift : Buffer.buffer Js.t -> unit Js.meth - method unshift_withString : Js.js_string Js.t -> unit Js.meth + method pause : readable_stream Js.t Js.meth - end + method pipe : 'self Js.t -> writable_stream Js.t Js.meth - let stream : stream Js.t = Bindings_utils.require_module "stream" + (* method pipe_with_options : *) + (* writable_stream Js.t -> *) + (* #stream Js.t Js.meth *) + + method read : 'a Js.t Js.opt Js.meth + + method read_withAmount : int -> 'a Js.t Js.opt Js.meth + + method resume : readable_stream Js.t Js.meth + method setEncoding : Js.js_string Js.t -> readable_stream Js.t Js.meth + + method unpipe : unit Js.meth + + method unpipe_withDest : writable_stream Js.t -> unit Js.meth + + method unshift : Buffer.buffer Js.t -> unit Js.meth + + method unshift_withString : Js.js_string Js.t -> unit Js.meth + end + + let stream : stream Js.t = Bindings_utils.require_module "stream" end module Net = struct - class type socket = object - (* Actually they are both read and writeable *) - inherit Stream.readable_stream - - method address : 'a Js.t Js.meth - method bufferSize : int Js.readonly_prop - method bytesRead : int Js.readonly_prop - method bytesWritten : int Js.readonly_prop - method connect : - Js.t + class type socket = + object + (* Actually they are both read and writeable *) + inherit Stream.readable_stream + + method address : 'a Js.t Js.meth + + method bufferSize : int Js.readonly_prop + + method bytesRead : int Js.readonly_prop + + method bytesWritten : int Js.readonly_prop + + method connect : + < port : int Js.prop + ; host : Js.js_string Js.t Js.prop + ; localAddress : Js.js_string Js.t Js.prop + ; localPort : int Js.prop + ; family : int Js.prop + ; hints : int Js.prop + ; lookup : 'a Js.callback Js.opt Js.t Js.prop > + Js.t + (* Many more connect methods needed *) (* method connect_with_listener : *) - method connecting : bool Js.t Js.readonly_prop - - method destroy : unit Js.meth - method destroy_with_exception : 'a Js.t -> unit Js.meth - method destroyed : bool Js.t Js.readonly_prop - method end_ : unit Js.meth - method end_with_data : Buffer.buffer Js.t -> unit Js.meth - method end_with_data_and_encoding : - Buffer.buffer Js.t -> Js.js_string Js.t -> unit Js.meth - method localAddress : Js.js_string Js.t Js.readonly_prop - method localPort : int Js.readonly_prop - (* method! pause : unit Js.meth *) - method ref : unit Js.meth - method remoteAddress : Js.js_string Js.t Js.readonly_prop - method remoteFamily : Js.js_string Js.t Js.readonly_prop - method remotePort : int Js.readonly_prop - (* method resume : unit Js.meth *) - (* method setEncoding : unit Js.meth *) - method setEncoding_with_encoding : Js.js_string Js.t -> unit Js.meth - method setKeepAlive : socket Js.t Js.meth - method setKeepAlive_withDelay : bool Js.t-> int -> socket Js.t Js.meth - method setNoDelay : socket Js.t Js.meth - method setNoDeplay_withBool : bool Js.t -> socket Js.t Js.meth - - method write : Buffer.buffer Js.t -> unit Js.meth - method write_withEncoding : - Buffer.buffer Js.t -> Js.js_string Js.t -> unit Js.meth - method write_withEncodingAndCallback : - Buffer.buffer Js.t -> Js.js_string Js.t -> 'a Js.callback -> unit Js.meth + method connecting : bool Js.t Js.readonly_prop - end + method destroy : unit Js.meth - and server = object - inherit Events.event_emitter - method address : Js.t Js.meth - method close : unit Js.meth - method close_withCallback : (error_arg -> unit) Js.callback Js.meth - method getConnections : (error_arg -> int -> unit) Js.callback Js.meth - (* Later type it, a server or socket *) - method listen : int -> (unit -> unit) Js.callback -> unit Js.meth - - method maxConnections : int Js.prop - method listening : bool Js.t Js.readonly_prop - end - and net = object - (* method connect : Js.t *) - method createServer : server Js.t Js.meth - method createServer_withConnListener : - (socket Js.t -> unit) Js.callback -> server Js.t Js.meth - method createServer_withOptionsAndConnListener : - Js.t -> - server Js.t Js.meth + method destroy_with_exception : 'a Js.t -> unit Js.meth - end + method destroyed : bool Js.t Js.readonly_prop + + method end_ : unit Js.meth + + method end_with_data : Buffer.buffer Js.t -> unit Js.meth + + method end_with_data_and_encoding : + Buffer.buffer Js.t -> Js.js_string Js.t -> unit Js.meth + + method localAddress : Js.js_string Js.t Js.readonly_prop + + method localPort : int Js.readonly_prop + + (* method! pause : unit Js.meth *) + method ref : unit Js.meth + + method remoteAddress : Js.js_string Js.t Js.readonly_prop + + method remoteFamily : Js.js_string Js.t Js.readonly_prop + + method remotePort : int Js.readonly_prop + + (* method resume : unit Js.meth *) + (* method setEncoding : unit Js.meth *) + method setEncoding_with_encoding : Js.js_string Js.t -> unit Js.meth + + method setKeepAlive : socket Js.t Js.meth + + method setKeepAlive_withDelay : bool Js.t -> int -> socket Js.t Js.meth + + method setNoDelay : socket Js.t Js.meth + + method setNoDeplay_withBool : bool Js.t -> socket Js.t Js.meth + + method write : Buffer.buffer Js.t -> unit Js.meth + + method write_withEncoding : + Buffer.buffer Js.t -> Js.js_string Js.t -> unit Js.meth + + method write_withEncodingAndCallback : + Buffer.buffer Js.t + -> Js.js_string Js.t + -> 'a Js.callback + -> unit Js.meth + end + + and server = + object + inherit Events.event_emitter + + method address : + < port : int Js.readonly_prop + ; family : Js.js_string Js.t Js.readonly_prop + ; address : Js.js_string Js.t Js.readonly_prop > + Js.t + Js.meth + + method close : unit Js.meth + + method close_withCallback : (error_arg -> unit) Js.callback Js.meth + + method getConnections : (error_arg -> int -> unit) Js.callback Js.meth + + (* Later type it, a server or socket *) + method listen : int -> (unit -> unit) Js.callback -> unit Js.meth + + method maxConnections : int Js.prop + + method listening : bool Js.t Js.readonly_prop + end + + and net = + object + (* method connect : Js.t *) + method createServer : server Js.t Js.meth + + method createServer_withConnListener : + (socket Js.t -> unit) Js.callback -> server Js.t Js.meth + + method createServer_withOptionsAndConnListener : + < allowHalfOpen : bool Js.t Js.prop + ; pauseOnConnect : bool Js.t Js.prop > + Js.t + -> server Js.t Js.meth + end (* let socket_with_options : 'a. *) (* ( < encoding : Js.js_string Js.t Js.Opt.t Js.readonly_prop + ; flag : Js.js_string Js.t Js.readonly_prop > + Js.t + -> Buffer.buffer Js.t Js.meth + + method readFile : + Js.js_string Js.t + -> (Js.error Js.t -> Buffer.buffer Js.t -> unit) Js.callback + -> unit Js.meth + end - class type fs = object - method readFileSync : - Js.js_string Js.t -> - Js.t -> - Buffer.buffer Js.t Js.meth + let fs : fs Js.t = Bindings_utils.require_module "fs" +end - method readFile : - Js.js_string Js.t -> - (Js.error Js.t -> Buffer.buffer Js.t -> unit) Js.callback -> - unit Js.meth +module Os = struct + class type os = + object + method _EOL : Js.js_string Js.t Js.readonly_prop - end + method arch : Js.js_string Js.t Js.meth - let fs : fs Js.t = Bindings_utils.require_module "fs" + (* method constants *) + method cpus : + < model : Js.js_string Js.t Js.readonly_prop + ; speed : int Js.readonly_prop + ; times : + < user : int Js.readonly_prop + ; nice : int Js.readonly_prop + ; sys : int Js.readonly_prop + ; idle : int Js.readonly_prop + ; irq : int Js.readonly_prop > + Js.t + Js.readonly_prop > + Js.t + Js.js_array + Js.t + Js.meth -end + method endianness : Js.js_string Js.t Js.meth -module Os = struct + method freemem : int Js.meth - class type os = object - method _EOL : Js.js_string Js.t Js.readonly_prop - method arch : Js.js_string Js.t Js.meth - (* method constants *) - method cpus : - Js.t Js.readonly_prop> - Js.t Js.js_array Js.t Js.meth - method endianness : Js.js_string Js.t Js.meth - method freemem : int Js.meth - method homedir : Js.js_string Js.t Js.meth - method hostname : Js.js_string Js.t Js.meth - method loadavg : int Js.js_array Js.t Js.meth + method homedir : Js.js_string Js.t Js.meth - end + method hostname : Js.js_string Js.t Js.meth - let os : os Js.t = Bindings_utils.require_module "os" + method loadavg : int Js.js_array Js.t Js.meth + end + let os : os Js.t = Bindings_utils.require_module "os" end module Http = struct + class type agent = + object + (* method createConnection *) + (* method destroy *) + (* method freeSockets *) + (* method getName *) + (* method maxFreeSockets *) + (* method maxSockets *) + (* method requests *) + (* method sockets *) + end + + class type client_request = + object + inherit Stream.writable_stream + end + + let agent : agent Js.t = (Bindings_utils.require_module "http") ##. Agent - class type agent = object - (* method createConnection *) - (* method destroy *) - (* method freeSockets *) - (* method getName *) - (* method maxFreeSockets *) - (* method maxSockets *) - (* method requests *) - (* method sockets *) - end + let agent_with_options : + ( < keepAlive : bool Js.t Js.readonly_prop + ; keepAliveMsecs : int Js.readonly_prop + ; maxSockets : int Js.readonly_prop + ; maxFreeSockets : int Js.readonly_prop > + Js.t + -> agent Js.t ) + Js.constr = + (Bindings_utils.require_module "http") ##. Agent +end - class type client_request = object - inherit Stream.writable_stream +class type process = + object + inherit Events.event_emitter - end + method argv : Js.js_string Js.t Js.js_array Js.t Js.readonly_prop - let agent : agent Js.t = (Bindings_utils.require_module "http")##.Agent + method abort : unit Js.meth - let agent_with_options : - ( Js.t -> agent Js.t) Js.constr = - (Bindings_utils.require_module "http")##.Agent + method arch : Js.js_string Js.t Js.readonly_prop -end + method chdir : Js.js_string Js.t -> unit Js.meth -class type process = object - inherit Events.event_emitter - method argv : Js.js_string Js.t Js.js_array Js.t Js.readonly_prop - method abort : unit Js.meth - method arch : Js.js_string Js.t Js.readonly_prop - method chdir : Js.js_string Js.t -> unit Js.meth - method config : 'a Js.t Js.readonly_prop - method connected : bool Js.t Js.readonly_prop - method cwd : Js.js_string Js.t Js.meth - method disconnect : unit Js.meth - method env : 'a Js.t Js.readonly_prop - (* Many more missing *) - method pid : int Js.readonly_prop - method exit : int -> unit Js.meth -end + method config : 'a Js.t Js.readonly_prop + + method connected : bool Js.t Js.readonly_prop + + method cwd : Js.js_string Js.t Js.meth + + method disconnect : unit Js.meth + + method env : 'a Js.t Js.readonly_prop + + (* Many more missing *) + method pid : int Js.readonly_prop + + method exit : int -> unit Js.meth + end -let process : process Js.t = Js.Unsafe.variable "process" +let process : process Js.t = Js.Unsafe.pure_js_expr "process" diff --git a/src/nodejs.mldylib b/src/nodejs.mldylib deleted file mode 100644 index dc9e55c..0000000 --- a/src/nodejs.mldylib +++ /dev/null @@ -1,4 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 4b00bbf4971bea7d45e4ee343c881d44) -Nodejs -# OASIS_STOP diff --git a/src/nodejs.mllib b/src/nodejs.mllib deleted file mode 100644 index dc9e55c..0000000 --- a/src/nodejs.mllib +++ /dev/null @@ -1,4 +0,0 @@ -# OASIS_START -# DO NOT EDIT (digest: 4b00bbf4971bea7d45e4ee343c881d44) -Nodejs -# OASIS_STOP diff --git a/src/nodejs_high_level.ml b/src/nodejs_high_level.ml new file mode 100644 index 0000000..30d1711 --- /dev/null +++ b/src/nodejs_high_level.ml @@ -0,0 +1,223 @@ +open Js_of_ocaml + +let __filename () = Nodejs.__filename () |> Js.to_string + +let __dirname () = Nodejs.__dirname () |> Js.to_string + +module Buffer = struct + type exn += Buffer_not_init + + class buffer ?(default = 1024) fresh_buffer = + object + val mutable buffer_handle : Nodejs.Buffer.buffer Js.t Js.opt = Js.null + + val mutable init_buffer = false + + initializer + if fresh_buffer then + buffer_handle <- + Nodejs.Buffer.buffer_static##alloc default |> Js.Opt.return; + init_buffer <- true + + method set_handle new_handle = + buffer_handle <- new_handle |> Js.Opt.return; + init_buffer <- true + + method to_string = + if init_buffer then begin + let b = Js.Opt.get buffer_handle (fun _ -> assert false) in + b##toString |> Js.to_string + end + else raise Buffer_not_init + end +end + +module Events = struct + class event_emmiter = + object + val k = new%js Nodejs.Events.event_emitter + + method event_names = + k##eventNames |> Js.str_array |> Js.to_array |> Array.map Js.to_string + |> Array.to_list + + method add_listener : 'a 'b. string -> ('a -> 'b) -> unit = + fun event_name callback -> + k##addListener (Js.string event_name) (Js.wrap_callback callback) + + method emit event_name = k##emit (Js.string event_name) + end +end + +module Fs = struct + type flag = + | Read + | Write + | Read_write + + let string_of_flag = function + | Read -> "r" + | Write -> "w" + | Read_write -> "rw" + + let read_file_sync file_name file_flag = + let result = + Nodejs.Fs.fs##readFileSync (Js.string file_name) + (object%js + val encoding = Js.null + + val flag = Js.string (string_of_flag file_flag) + end ) + in + result##toString |> Js.to_string + + let read_file_async file_name callback = + let wrapped err given_buffer = + let b = new Buffer.buffer false in + b#set_handle given_buffer; + callback err b + in + Nodejs.Fs.fs##readFile (Js.string file_name) (Js.wrap_callback wrapped) +end + +module Net = struct + type exn += Socket_not_init + + class socket fresh_socket = + object + val mutable socket_handle : Nodejs.Net.socket Js.t Js.opt = Js.null + + val mutable init_socket = false + + initializer + if fresh_socket then + socket_handle <- new%js Nodejs.Net.socket |> Js.Opt.return; + init_socket <- true + + method set_handle raw_socket = + socket_handle <- raw_socket |> Js.Opt.return; + init_socket <- true + + method write data = + if init_socket then begin + let c = Js.Opt.get socket_handle (fun _ -> assert false) in + c##write (Nodejs.Buffer.buffer_static##from (Js.string data)) + end + else raise Socket_not_init + end + + class server listener = + object + val k = + let wrapped given_socket = + let s = new socket false in + s#set_handle given_socket; + listener s + in + Nodejs.Net.net##createServer_withConnListener (Js.wrap_callback wrapped) + + (* method listen : 'a 'b. int -> ('a -> 'b) -> unit = fun port callback -> *) + (* k##listen port (Js.wrap_callback callback) *) + method listen port callback = k##listen port (Js.wrap_callback callback) + end + + (* let create_server listener = *) + (* Nodejs.Net.net##createServer_withConnListener (Js.wrap_callback listener) *) +end + +module Os = struct + (* Later hide with mli *) + let os_handle = Nodejs.Os.os + + type cpu_stat = + { model : string + ; speed : int + ; times : < user : int ; nice : int ; sys : int ; idle : int ; irq : int > + } + + type endianess = + | Big + | Little + + type platform = + | Aix + | Darwin + | Freebsd + | Linux + | Openbsd + | Sunos + | Win32 + | Android + + (* Hide as well in mli *) + let endianess_of_string = function + | "BE" -> Big + | "LE" -> Little + | _ -> assert false + + let platform_of_string = function + | "aix" -> Aix + | "darwin" -> Darwin + | "freebsd" -> Freebsd + | "linux" -> Linux (* TODO FINISH*) + | _ -> assert false + + let cpu_stats () = + os_handle##cpus |> Js.to_array + |> Array.map (fun c -> + { model = c##.model |> Js.to_string + ; speed = c##.speed + ; times = + object + method user = c##.times##.user + + method nice = c##.times##.nice + + method sys = c##.times##.sys + + method idle = c##.times##.idle + + method irq = c##.times##.irq + end + } ) + |> Array.to_list + + let endianess () = + os_handle##endianness |> Js.to_string |> endianess_of_string + + let free_memory_available () = os_handle##freemem + + let home_directory () = os_handle##homedir |> Js.to_string + + let hostname () = os_handle##hostname |> Js.to_string + + let load_average () = os_handle##loadavg |> Js.to_array +end + +class process = + object + val js = Nodejs.process + + method arguments = + js##.argv |> Js.to_array |> Array.map Js.to_string |> Array.to_list + + method abort = js##abort + + method arch = js##.arch |> Js.to_string + + method change_dir s = js##chdir (Js.string s) + + method config : 'a. 'a Js.t = js##.config + + method connect = js##.connected |> Js.to_bool + + method current_working_directory = js##cwd |> Js.to_string + + method environment_variables : 'b. 'b Js.t = js##.env + + method pid = js##.pid + + method exit e = js##exit e + end + +let process = new process diff --git a/src/nodejs_high_level_lwt.ml b/src/nodejs_high_level_lwt.ml new file mode 100644 index 0000000..3095ccd --- /dev/null +++ b/src/nodejs_high_level_lwt.ml @@ -0,0 +1,7 @@ +module Fs = struct + let read_file filename = + let t, w = Lwt.task () in + Nodejs_high_level.Fs.read_file_async filename (fun err content -> + Lwt.wakeup w (err, content) ); + t +end