aboutsummaryrefslogtreecommitdiff
path: root/luxc/src/lux/compiler/cache.clj
blob: 5f0a77c65b503c3a0b8c412058a49eca2c410c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;;  Copyright (c) Eduardo Julian. All rights reserved.
;;  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
;;  If a copy of the MPL was not distributed with this file,
;;  You can obtain one at http://mozilla.org/MPL/2.0/.

(ns lux.compiler.cache
  (:refer-clojure :exclude [load])
  (:require [clojure.string :as string]
            [clojure.java.io :as io]
            clojure.core.match
            clojure.core.match.array
            (lux [base :as & :refer [|do return* return fail fail* |case |let]]
                 [type :as &type]
                 [host :as &host])
            [lux.host.generics :as &host-generics]
            (lux.analyser [base :as &a]
                          [module :as &a-module]
                          [meta :as &a-meta])
            (lux.compiler [base :as &&]
                          [io :as &&io])
            (lux.compiler.cache [type :as &&&type]
                                [ann :as &&&ann]))
  (:import (java.io File
                    BufferedOutputStream
                    FileOutputStream)
           (java.lang.reflect Field)))

;; [Utils]
(defn ^:private read-file [^File file]
  "(-> File (Array Byte))"
  (with-open [reader (io/input-stream file)]
    (let [length (.length file)
          buffer (byte-array length)]
      (.read reader buffer 0 length)
      buffer)))

(defn ^:private clean-file [^File file]
  "(-> File (,))"
  (doseq [^File f (seq (.listFiles file))
          :when (not (.isDirectory f))]
    (.delete f)))

(defn ^:private get-field [^String field-name ^Class class]
  "(-> Text Class Object)"
  (-> class ^Field (.getField field-name) (.get nil)))

;; [Resources]
(def module-class (str &/module-class-name ".class"))

(defn cached? [module]
  "(-> Text Bool)"
  (.exists (new File (str @&&/!output-dir "/" (&host/->module-class module) "/" module-class)))
  ;; false
  )

(defn delete [module]
  "(-> Text (Lux Null))"
  (fn [state]
    (do (clean-file (new File (str @&&/!output-dir "/" (&host/->module-class module))))
      (return* state nil))))

(defn ^:private module-dirs
  "(-> File (clojure.Seq File))"
  [^File module]
  (->> module
       .listFiles
       (filter #(.isDirectory %))
       (map module-dirs)
       (apply concat)
       (list* module)))

(defn clean [state]
  "(-> Compiler Null)"
  (let [needed-modules (->> state (&/get$ &/$modules) &/|keys &/->seq set)
        output-dir-prefix (str (.getAbsolutePath (new File @&&/!output-dir)) "/")
        outdated? #(->> % (contains? needed-modules) not)
        outdated-modules (->> (new File @&&/!output-dir)
                              .listFiles (filter #(.isDirectory %))
                              (map module-dirs) doall (apply concat)
                              (map #(-> ^File % .getAbsolutePath (string/replace output-dir-prefix "")))
                              (filter outdated?))]
    (doseq [^String f outdated-modules]
      (clean-file (new File (str output-dir-prefix f))))
    nil))

(defn ^:private install-all-classes-in-module [!classes module* ^String module-path]
  (doseq [^File file (seq (.listFiles (File. module-path)))
          :when (not (.isDirectory file))
          :let [file-name (.getName file)]
          :when (not= module-class file-name)]
    (let [real-name (second (re-find #"^(.*)\.class$" file-name))
          bytecode (read-file file)]
      (swap! !classes assoc (str module* "." real-name) bytecode))))

(defn ^:private assume-async-result
  "(-> (Error Compiler) (Lux Null))"
  [result]
  (fn [_]
    (|case result
      (&/$Left error)
      (&/$Left error)

      (&/$Right compiler)
      (return* compiler nil))))

(defn load [source-dirs module module-hash compile-module]
  "(-> (List Text) Text Int (-> Text (Lux (,))) (Lux Bool))"
  (|do [already-loaded? (&a-module/exists? module)]
    (if already-loaded?
      (return module-hash)
      (|let [redo-cache (|do [_ (delete module)
                              ;; async (compile-module module)
                              ]
                          ;; (assume-async-result @async)
                          (compile-module module))]
        (if (cached? module)
          (|do [loader &/loader
                !classes &/classes
                :let [module* (&host-generics/->class-name module)
                      module-path (str @&&/!output-dir "/" module)
                      class-name (str module* "._")
                      old-classes @!classes
                      ^Class module-class (do (swap! !classes assoc class-name (read-file (File. (str module-path "/_.class"))))
                                            (&&/load-class! loader class-name))
                      _ (install-all-classes-in-module !classes module* module-path)]]
            (if (and (= module-hash (get-field &/hash-field module-class))
                     (= &/compiler-version (get-field &/compiler-field module-class)))
              (|do [^String descriptor (&&/read-module-descriptor! module)
                    :let [sections (.split descriptor &&/section-separator)
                          [^String imports-section ^String tags-section module-anns-section ^String defs-section] sections
                          imports (vec (.split imports-section &&/entry-separator))]
                    loads (&/map% (fn [^String _import]
                                    (let [[_module _hash] (.split _import &&/datum-separator 2)]
                                      (|do [file-content (&&io/read-file source-dirs (str _module ".lux"))
                                            :let [file-hash (hash file-content)
                                                  __hash (Integer/parseInt _hash)]
                                            _ (load source-dirs _module file-hash compile-module)
                                            cached? (&/cached-module? _module)
                                            :let [consistent-cache? (= file-hash __hash)]]
                                        (return (and cached?
                                                     consistent-cache?)))))
                                  (if (= [""] imports)
                                    &/$Nil
                                    (&/->list imports)))]
                (if (->> loads &/->seq (every? true?))
                  (|do [:let [tag-groups (if (= "" tags-section)
                                           &/$Nil
                                           (-> tags-section
                                               (.split &&/entry-separator)
                                               seq
                                               (->> (map (fn [^String _group]
                                                           (let [[_type & _tags] (.split _group &&/datum-separator)]
                                                             (&/T [_type (->> _tags seq &/->list)])))))
                                               &/->list))]
                        _ (&a-module/create-module module module-hash)
                        _ (&a-module/set-anns (&&&ann/deserialize-anns module-anns-section) module)
                        _ (&/flag-cached-module module)
                        _ (&a-module/set-imports imports)
                        :let [desc-defs (vec (.split defs-section &&/entry-separator))]
                        _ (&/map% (fn [^String _def-entry]
                                    (let [parts (.split _def-entry &&/datum-separator)]
                                      (case (alength parts)
                                        2 (let [[_name _alias] parts
                                                [_ __module __name] (re-find #"^(.*);(.*)$" _alias)
                                                def-class (&&/load-class! loader (str (&host-generics/->class-name __module) "." (&host/def-name __name)))
                                                def-anns (&/|list (&/T [&a-meta/alias-tag (&/$IdentM (&/T [__module __name]))]))
                                                def-value (get-field &/value-field def-class)]
                                            (|do [def-type (&a-module/def-type __module __name)]
                                              (&a-module/define module _name def-type def-anns def-value)))
                                        3 (let [[_name _type _anns] parts
                                                def-class (&&/load-class! loader (str module* "." (&host/def-name _name)))
                                                [def-anns _] (&&&ann/deserialize-anns _anns)
                                                [def-type _] (&&&type/deserialize-type _type)
                                                def-value (get-field &/value-field def-class)]
                                            (&a-module/define module _name def-type def-anns def-value)))))
                                  (if (= [""] desc-defs)
                                    &/$Nil
                                    (&/->list desc-defs)))
                        _ (&/map% (fn [group]
                                    (|let [[_type _tags] group]
                                      (|do [[was-exported? =type] (&a-module/type-def module _type)]
                                        (&a-module/declare-tags module _tags was-exported? =type))))
                                  tag-groups)]
                    (return module-hash))
                  redo-cache))
              (do (reset! !classes old-classes)
                redo-cache)))
          redo-cache)))))