|
1813 | 1813 | (str n ".")))]
|
1814 | 1814 | `(~s ~@args))))
|
1815 | 1815 |
|
| 1816 | +(defn push-thread-bindings |
| 1817 | + "Takes a map of Var/value pairs and applies the given value to the Var in the |
| 1818 | + current thread. |
| 1819 | + |
| 1820 | + This call should be accompanied with a pop-thread-bindings call in a |
| 1821 | + try/finally block. |
| 1822 | + |
| 1823 | + This function is a very low level function and its use is discouraged in favor |
| 1824 | + of a higher level construct like the binding macros." |
| 1825 | + [bindings] |
| 1826 | + (basilisp.lang.runtime/push-thread-bindings bindings)) |
| 1827 | + |
| 1828 | +(defn pop-thread-bindings |
| 1829 | + "Pop thread bindings set by a corresponding call to push-thread-bindings. This |
| 1830 | + should not be called without a prior call to push-thread-bindings." |
| 1831 | + [] |
| 1832 | + (basilisp.lang.runtime/pop-thread-bindings)) |
| 1833 | + |
1816 | 1834 | (defmacro binding
|
1817 | 1835 | "Establish thread-local bindings for the vars given. The bindings are guaranteed
|
1818 | 1836 | to clear once execution passes outside the scope of this block."
|
|
1823 | 1841 | (throw
|
1824 | 1842 | (ex-info "Expected an even number of bindings"
|
1825 | 1843 | {:bindings bindings})))
|
1826 |
| - (let [vvar (first bindings) |
1827 |
| - val (second bindings)] |
| 1844 | + (let [var-bindings (reduce (fn [v pair] |
| 1845 | + (let [vvar (first pair) |
| 1846 | + vval (second pair)] |
| 1847 | + (conj v `(var ~vvar) vval))) |
| 1848 | + [] |
| 1849 | + (partition 2 bindings))] |
1828 | 1850 | `(try
|
1829 | 1851 | (do
|
1830 |
| - (. (var ~vvar) (~'push-bindings ~val)) |
1831 |
| - ~@(if (nthnext bindings 2) |
1832 |
| - [(concat |
1833 |
| - (list 'binding (vec (nthrest bindings 2))) |
1834 |
| - body)] |
1835 |
| - body)) |
| 1852 | + (push-thread-bindings (hash-map ~@var-bindings)) |
| 1853 | + ~@body) |
1836 | 1854 | (finally
|
1837 |
| - (. (var ~vvar) ~'pop-bindings))))) |
| 1855 | + (pop-thread-bindings))))) |
1838 | 1856 |
|
1839 | 1857 | (import* [time :as py-time])
|
1840 | 1858 |
|
|
2194 | 2212 | ctx
|
2195 | 2213 | module)))
|
2196 | 2214 |
|
| 2215 | +;;;;;;;;;;;;;;;;;;; |
| 2216 | +;; Var Utilities ;; |
| 2217 | +;;;;;;;;;;;;;;;;;;; |
| 2218 | + |
| 2219 | +(defn alter-var-root |
| 2220 | + "Atomically alter the Var root by calling (apply f root args) and setting |
| 2221 | + the root as the result." |
| 2222 | + [v f & args] |
| 2223 | + (apply-method v alter-root f args)) |
| 2224 | + |
| 2225 | +(defn find-var |
| 2226 | + "Return the Var named by namespace-qualified sym if it exists, or nil otherwise." |
| 2227 | + [sym] |
| 2228 | + (basilisp.lang.runtime.Var/find sym)) |
| 2229 | + |
| 2230 | +(defn thread-bound? |
| 2231 | + "Return true if vars are thread-bound, which implies that a set! will succeed. |
| 2232 | + Returns true if no vars are given." |
| 2233 | + [& vars] |
| 2234 | + (loop [v (first vars) |
| 2235 | + r (rest vars)] |
| 2236 | + (if-not v |
| 2237 | + true |
| 2238 | + (if (.-is-thread-bound v) |
| 2239 | + (recur (first r) (rest r)) |
| 2240 | + false)))) |
| 2241 | + |
| 2242 | +(defn var-get |
| 2243 | + "Return the value inside the Var. Return thread local bindings if they exist, |
| 2244 | + otherwise, return the root binding." |
| 2245 | + [v] |
| 2246 | + @v) |
| 2247 | + |
| 2248 | +(defn var-set |
| 2249 | + "Set the binding of the Var. Must be thread-local." |
| 2250 | + [v val] |
| 2251 | + (if (thread-bound? v) |
| 2252 | + (set! (.-value v) val) |
| 2253 | + (throw |
| 2254 | + (ex-info "Cannot set non-thread-bound Var binding" {:var v})))) |
| 2255 | + |
2197 | 2256 | ;;;;;;;;;;;;;;;;;;;;;;;;;
|
2198 | 2257 | ;; Namespace Utilities ;;
|
2199 | 2258 | ;;;;;;;;;;;;;;;;;;;;;;;;;
|
2200 | 2259 |
|
| 2260 | +(defn all-ns |
| 2261 | + "Return a sequence of all namespaces." |
| 2262 | + [] |
| 2263 | + (vals @basilisp.lang.runtime.Namespace/-NAMESPACES)) |
| 2264 | + |
| 2265 | +(defn find-ns |
| 2266 | + "Return the namespace named by sym if it exists, or nil otherwise." |
| 2267 | + [sym] |
| 2268 | + (basilisp.lang.runtime.Namespace/get sym)) |
| 2269 | + |
2201 | 2270 | (defn the-ns
|
2202 | 2271 | "If v is a symbol, return the Namespace named by that symbol if it
|
2203 | 2272 | exists. If v is a Namespace, return it. Otherwise, throw an exception."
|
|
2215 | 2284 | {:value v
|
2216 | 2285 | :type (python/type v)}))))
|
2217 | 2286 |
|
| 2287 | +(defn intern |
| 2288 | + "Finds or creates a Var in ns (which is either a namespace or symbol), |
| 2289 | + setting the root binding to val, if provided. The namespace must exist. |
| 2290 | + Return the Var." |
| 2291 | + ([ns name] |
| 2292 | + (let [ns (the-ns ns) |
| 2293 | + v (basilisp.lang.runtime/Var ns name)] |
| 2294 | + (.intern ns v))) |
| 2295 | + ([ns name val] |
| 2296 | + (let [ns (the-ns ns) |
| 2297 | + v (basilisp.lang.runtime/Var ns name)] |
| 2298 | + (set! (.-root v) val) |
| 2299 | + (.intern ns v)))) |
| 2300 | + |
2218 | 2301 | (defn create-ns
|
2219 | 2302 | "Create a Namespace with the name ns-sym or return the existing one
|
2220 | 2303 | if it already exists."
|
2221 | 2304 | [ns-sym]
|
2222 |
| - (.get-or-create basilisp.lang.runtime/Namespace ns-sym)) |
| 2305 | + (basilisp.lang.runtime.Namespace/get-or-create ns-sym)) |
| 2306 | + |
| 2307 | +(defn remove-ns |
| 2308 | + "Remove the namespace named by the symbol." |
| 2309 | + [ns-sym] |
| 2310 | + (basilisp.lang.runtime.Namespace/remove ns-sym)) |
| 2311 | + |
| 2312 | +(defn ns-name |
| 2313 | + "Return the name of the namespace, a symbol." |
| 2314 | + [ns] |
| 2315 | + (.-name ns)) |
2223 | 2316 |
|
2224 | 2317 | (defn ns-aliases
|
2225 | 2318 | "Return a map of Basilisp namespaces which are aliased in the current
|
|
2232 | 2325 | m
|
2233 | 2326 | (assoc m alias namespace))))
|
2234 | 2327 | {}
|
2235 |
| - (.-aliases ns))) |
| 2328 | + (.-aliases (the-ns ns)))) |
2236 | 2329 |
|
2237 | 2330 | (defn ns-imports
|
2238 | 2331 | "Return a set of Python modules which are imported in the current
|
2239 | 2332 | namespace."
|
2240 | 2333 | [ns]
|
2241 |
| - (.-imports ns)) |
| 2334 | + (.-imports (the-ns ns))) |
2242 | 2335 |
|
2243 | 2336 | (defn ns-interns
|
2244 | 2337 | "Return a map of symbols to Vars which are interned in the current
|
2245 | 2338 | namespace."
|
2246 | 2339 | [ns]
|
2247 |
| - (.-interns ns)) |
| 2340 | + (.-interns (the-ns ns))) |
| 2341 | + |
| 2342 | +(defn ns-unalias |
| 2343 | + "Remove the alias for the symbol sym from ns. Return nil." |
| 2344 | + [ns sym] |
| 2345 | + (.remove-alias (the-ns ns) sym)) |
| 2346 | + |
| 2347 | +(defn ns-unmap |
| 2348 | + "Remove the mapping for the symbol sym from ns. Return nil." |
| 2349 | + [ns sym] |
| 2350 | + (.unmap (the-ns ns) sym)) |
2248 | 2351 |
|
2249 | 2352 | (defn ns-publics
|
2250 | 2353 | "Return a map of symbols to public Vars which are interned in the
|
2251 | 2354 | current namespace.
|
2252 | 2355 |
|
2253 | 2356 | Public vars are Vars which are declared without :private metadata."
|
2254 | 2357 | [ns]
|
2255 |
| - (let [interns (ns-interns ns)] |
| 2358 | + (let [interns (ns-interns (the-ns ns))] |
2256 | 2359 | (if-not (seq interns)
|
2257 | 2360 | {}
|
2258 | 2361 | (reduce (fn [m entry]
|
|
2266 | 2369 | "Return a map of symbols to Vars which are referred in the current
|
2267 | 2370 | namespace."
|
2268 | 2371 | [ns]
|
2269 |
| - (.-refers ns)) |
| 2372 | + (.-refers (the-ns ns))) |
2270 | 2373 |
|
2271 | 2374 | (defn ns-map
|
2272 | 2375 | "Return a map of all the mapped symbols in the namespace.
|
2273 | 2376 |
|
2274 | 2377 | Includes the return values of ns-interns and ns-refers in one map."
|
2275 | 2378 | ([] (ns-map *ns*))
|
2276 | 2379 | ([ns]
|
2277 |
| - (merge |
2278 |
| - (ns-interns ns) |
2279 |
| - (ns-refers ns)))) |
| 2380 | + (let [resolved-ns (the-ns ns)] |
| 2381 | + (merge |
| 2382 | + (ns-interns resolved-ns) |
| 2383 | + (ns-refers resolved-ns))))) |
2280 | 2384 |
|
2281 | 2385 | (defn ns-resolve
|
2282 | 2386 | "Return the Var which will be resolved by the symbol in the given namespace."
|
2283 | 2387 | [ns sym]
|
2284 |
| - (basilisp.lang.runtime/resolve-var sym ns)) |
| 2388 | + (basilisp.lang.runtime/resolve-var sym (the-ns ns))) |
2285 | 2389 |
|
2286 | 2390 | (defn resolve
|
2287 | 2391 | "Return the Var which will be resolved by the symbol in the namespace currently
|
|
2339 | 2443 | (recur (rest refers)))))]
|
2340 | 2444 | (do-refer interns)))))
|
2341 | 2445 |
|
| 2446 | +(def refer-basilisp |
| 2447 | + "Refer Vars from basilisp.core using the same filter syntax as refer." |
| 2448 | + (partial refer 'basilisp.core)) |
| 2449 | + |
| 2450 | +(def refer-clojure |
| 2451 | + "Compatibility layer with JVM Clojure, which points to refer-basilisp." |
| 2452 | + refer-basilisp) |
| 2453 | + |
2342 | 2454 | (import importlib)
|
2343 | 2455 |
|
2344 | 2456 | (defn ^:private add-refers
|
|
3333 | 3445 | ((.- ~type-name ~'create) m#))
|
3334 | 3446 |
|
3335 | 3447 | ~type-name)))
|
| 3448 | + |
0 commit comments