Browse Source

OCaml hot reloading

rexim 6 years ago
parent
commit
8c96ebaddf

+ 13 - 9
TODO.org

@@ -1,26 +1,30 @@
-- [-] Vector Graphics
+- [X] Vector Graphics
   - [X] cairo
   - [X] cairo
     - [X] [[https://www.cairographics.org/][cairographics.org]]
     - [X] [[https://www.cairographics.org/][cairographics.org]]
       - [X] bezier curve
       - [X] bezier curve
         - [X] [[file:cairo-probe/][file:./cairo-probe/]]
         - [X] [[file:cairo-probe/][file:./cairo-probe/]]
           - [X] They work quite well
           - [X] They work quite well
-  - [ ] ...
-- [ ] Fast feedback loop preview
+- [-] Fast feedback loop preview
   - [ ] SDL
   - [ ] SDL
     - [ ] [[https://www.libsdl.org/download-2.0.php][Simple DirectMedia Layer - SDL version 2.0.9 (stable)]]
     - [ ] [[https://www.libsdl.org/download-2.0.php][Simple DirectMedia Layer - SDL version 2.0.9 (stable)]]
       - [ ] [[file:cairo-probe/][file:./cairo-probe/]]
       - [ ] [[file:cairo-probe/][file:./cairo-probe/]]
         - [ ] The performace of SDL+cairo is not clear yet
         - [ ] The performace of SDL+cairo is not clear yet
-  - [ ] ...
-- [-] Functional Programming with Static typing
+  - [X] Hot reload of OCaml code
+    - [X] [[file:ocaml-hotreload/][file:./ocaml-hotreload/]]
+      - [X] [[https://jaredforsyth.com/posts/hot-reloading-ocaml-on-web-desktop-and-android/][Hot-reloading OCaml on Web, Desktop, and Android | Jared Forsyth.com]]
+        - [X] Dynlink.load does not exist
+      - [X] [[http://zderadicka.eu/plugins-in-ocaml-with-dynlink-library/][Plugins in OCAML with Dynlink library | Ivanovo]]
+        - [X] Works perfectly!
+- [X] Functional Programming with Static typing
   - [X] OCaml
   - [X] OCaml
     - [X] [[https://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.html][How to wrap C functions to OCaml]]
     - [X] [[https://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.html][How to wrap C functions to OCaml]]
       - [X] [[file:ocaml-c/][file:./ocaml-c/]]
       - [X] [[file:ocaml-c/][file:./ocaml-c/]]
         - [X] Too complicated
         - [X] Too complicated
-  - [-] MLton
-    - [-] [[http://www.mlton.org/][MLton]]
-      - [-] [[file:mlton-c/][file:./mlton-c/]]
+          - [X] Not as complicated if use it purely for C wrapping.
+  - [X] MLton
+    - [X] [[http://www.mlton.org/][MLton]]
+      - [X] [[file:mlton-c/][file:./mlton-c/]]
         - [X] Compilation is extremely slow.
         - [X] Compilation is extremely slow.
-        - [ ] 
 - [ ] Quick video rendering
 - [ ] Quick video rendering
   - [ ] [[https://gstreamer.freedesktop.org/][GStreamer: open source multimedia framework]]
   - [ ] [[https://gstreamer.freedesktop.org/][GStreamer: open source multimedia framework]]
   - [ ] [[https://ffmpeg.org/libavcodec.html][Libavcodec Documentation]]
   - [ ] [[https://ffmpeg.org/libavcodec.html][Libavcodec Documentation]]

+ 4 - 0
ocaml-hotreload/.gitignore

@@ -0,0 +1,4 @@
+*.cma
+*.cmi
+*.cmo
+probe

+ 13 - 0
ocaml-hotreload/Makefile

@@ -0,0 +1,13 @@
+all: probe test_animation.cmo
+
+test_animation.cmo: animation.cma test_animation.ml
+	ocamlc -c animation.cma test_animation.ml
+
+animation.cma: animation.cmo
+	ocamlc -a -o animation.cma animation.cmo
+
+animation.cmo: animation.ml
+	ocamlc -c animation.ml
+
+probe: animation.cma main.ml
+	ocamlfind ocamlc -o probe -linkpkg -package dynlink animation.cma main.ml

+ 10 - 0
ocaml-hotreload/animation.ml

@@ -0,0 +1,10 @@
+module type Anim =
+  sig
+    val render_frame: unit -> unit
+  end
+
+let ca : (module Anim) option ref = ref None
+let get_ca ()  =
+  match !ca with
+  | Some a -> a
+  | None -> failwith "No animation loaded"

+ 18 - 0
ocaml-hotreload/main.ml

@@ -0,0 +1,18 @@
+open Animation
+
+let rec eventLoop (n: int) =
+  let module A = (val Animation.get_ca() : Anim) in
+  A.render_frame ();
+  if n > 10
+  then
+    begin
+      Dynlink.loadfile("./test_animation.cmo");
+      print_endline "Reloaded";
+      eventLoop 0
+    end
+  else
+    eventLoop (n + 1)
+
+let _ =
+  Dynlink.loadfile("./test_animation.cmo");
+  eventLoop 0

+ 9 - 0
ocaml-hotreload/test_animation.ml

@@ -0,0 +1,9 @@
+open Animation
+
+module TestAnim: Anim =
+  struct
+    let render_frame () = print_endline "Hello"
+  end
+
+let () =
+  ca := Some (module TestAnim : Anim)