Skip to content

Commit

Permalink
resuscitate last attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrand committed Feb 14, 2025
1 parent 9badd7d commit 33a0029
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 7 deletions.
37 changes: 34 additions & 3 deletions clj/src/cljd/build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[clojure.tools.deps :as deps]
[clojure.string :as str]
[clojure.stacktrace :as st]
[clojure.java.io :as io]))
[clojure.java.io :as io]
[clojure.core.server :as server]))

(def ^:dynamic *ansi* false)
(def ^:dynamic *deps*)
Expand Down Expand Up @@ -203,6 +204,25 @@
(finally
(run! remove-tap fns#)))))

(defn repl [{:keys [dart-version analyzer-info recompile-count trigger-reload]}]
(binding [compiler/*dart-version* dart-version
compiler/analyzer-info analyzer-info]
(try
(println "ClojureDart")
(binding [compiler/*current-ns* 'sample.counter]
(compiler/recompile-form '(ns cljd.user) (swap! recompile-count inc))
(loop []
(print (str compiler/*current-ns* "=> "))
(flush)
(let [expr (read)]
; how to evaluate in one ns?
(compiler/recompile-form expr (swap! recompile-count inc))
(trigger-reload)
(recur))))
(catch Exception e
(println "REPL session terminated." (.getMessage e))
(st/print-stack-trace e)))))

(defn compile-cli
[& {:keys [watch namespaces flutter] :or {watch false}}]
(let [user-dir (System/getProperty "user.dir")
Expand Down Expand Up @@ -234,7 +254,7 @@
(when root paths))
(vals (:libs *deps*)))))
dirty-nses (volatile! #{})
recompile-count (volatile! 0)
recompile-count (atom 0)
compile-nses
(fn [nses]
(let [nses (into @dirty-nses nses)]
Expand All @@ -244,7 +264,7 @@
(println (title "Compiling to Dart...") (timestamp))
(run! #(println " " %) (sort nses))
(try
(compiler/recompile nses (vswap! recompile-count inc))
(compiler/recompile nses (swap! recompile-count inc))
(println (success) (timestamp))
true
(catch Exception e
Expand Down Expand Up @@ -329,6 +349,17 @@
(recur state))))))))
(.setDaemon true)
.start))
(let [^java.net.ServerSocket socket
(server/start-server {:port 0 :name "CLJD repl" :accept 'cljd.build/repl
:args [{:dart-version compiler/*dart-version*
:analyzer-info compiler/analyzer-info
:recompile-count recompile-count
:trigger-reload
#(doto flutter-stdin
(.write "r")
.flush)}]})]
(println (title "ClojureDart REPL (experimental 💥)") "listening on port" (title (.getLocalPort socket)))
(newline))
(watch-dirs-until (fn [_] (some-> p .isAlive not)) nil dirs (compile-files flutter-stdin))
(when p
(println (str "💀 Flutter sub-process exited with " (.exitValue p)))))
Expand Down
68 changes: 65 additions & 3 deletions clj/src/cljd/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4670,9 +4670,10 @@
(try

(let [contributions
(for [ns nses-to-recompile
path (keys (:contributions-paths (nses-before ns)))]
path)]
(set
(for [ns nses-to-recompile
path (keys (:contributions-paths (nses-before ns)))]
path))]
; undo contributions
(run! (fn [path]
(assert (vector? path))
Expand All @@ -4699,6 +4700,67 @@
(reset! nses nses-before) ; avoid messy states
(throw e))))))

(defn recompile-form
[form recompile-count]
(with-dump-modified-files
(let [current-ns *current-ns*
nses-before @nses
dependants (fn [ns]
(let [lib (some-> nses-before ns :lib)]
(for [[ns {:keys [imports contribs]}] nses-before
:when (and (get imports lib) (not (get contribs lib)))]
ns)))
nses-to-recompile
(disj (transitive-closure [current-ns] dependants) current-ns)]
; COMPILE EXPR
(try
(binding [*recompile-count* recompile-count]
(binding [*locals-gen* {}
*dart-out* *out*]
(try
(case (when (seq? form) (first form))
ns (do
(emit form {})
(emit `(cljd.core/extend-protocol cljd.flutter.repl/ReplHack
dart:core/Object
(~'-form-exec [_#]
(dart:core/print (cljd.core/pr-str nil)))) {}))
(emit `(cljd.core/extend-protocol cljd.flutter.repl/ReplHack
dart:core/Object
(~'-form-exec [_#]
(dart:core/print (cljd.core/pr-str ~form)))) {}))
(catch Exception e
(prn e)
(throw e))))
(let [contributions
(set (for [ns nses-to-recompile
path (keys (:contributions-paths (nses-before ns)))]
path))]
; undo contributions
(run! (fn [path]
(assert (vector? path))
(swap! nses update-in (pop path) dissoc (peek path)))
contributions)

; remove nses to be recompiled -- but not their libs to keep aliases stable
(apply swap! nses dissoc nses-to-recompile)

(doseq [[protocol-ns protocol-name tag] contributions]
(case tag
:extensions
(when-some [proto-map (get-in @nses [protocol-ns protocol-name])]
(binding [*current-ns* protocol-ns
*locals-gen* {}]
(emit (expand-protocol-impl proto-map) {}))))))
(doseq [ns nses-to-recompile
; the ns may already have been transitively reloaded
:when (nil? (@nses ns))]
; recompiling via ns and not via url because cljd/cljc shadowing
(compile-namespace ns)))
(catch Exception e
(reset! nses nses-before) ; avoid messy states
(throw e))))))

(comment

(do
Expand Down
21 changes: 20 additions & 1 deletion clj/src/cljd/flutter.cljd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
["package:flutter/material.dart" :as material]
["package:flutter/foundation.dart" :as foundation]
["dart:async" :as dart:async]
[cljd.string :as str])
[cljd.string :as str]
[cljd.flutter.repl :as repl])
(:host-ns
(:require [clojure.string :as str])))

Expand Down Expand Up @@ -948,6 +949,24 @@
`(fn [~closest-context] (-widget-cont {:closest-ctx true} ~argsvec? ~@body))))

(defmacro run
"Starts an application, takes a widget body and uses it as the application root widget.
Hot-reload friendly."
[& widget-body]
`(widgets/runApp
#_~(stateless-flush {} widget-body)
(reify :extends (widgets/StatefulWidget)
:no-meta true
(~'createState [_#]
(reify :extends (widgets/State)
:no-meta true
(~'reassemble [this#]
(.reassemble ^{:tag ~'super} this#)
(repl/-form-exec (Object)))
(~'build [_# ~closest-context]
(-widget-cont {:closest-ctx true} ~@widget-body)))))))


#_(defmacro run
"Starts an application, takes a widget body and uses it as the application root widget.
Hot-reload friendly."
[& widget-body]
Expand Down

0 comments on commit 33a0029

Please sign in to comment.