Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
Correctly set __dirname and __filename when running script file (#…
Browse files Browse the repository at this point in the history
…402)

fixes #185
  • Loading branch information
anmonteiro committed May 14, 2018
1 parent d22464e commit 658732f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- ctrl-c during REPL execution exits lumo ([#327](https://github.com/anmonteiro/lumo/issues/327)).
- SIGINT cannot be caught ([#191](https://github.com/anmonteiro/lumo/issues/191)).
- Correctly set `__dirname` and `__filename` when running script file ([#185](https://github.com/anmonteiro/lumo/issues/185)).

## [1.9.0-alpha](https://github.com/anmonteiro/lumo/compare/1.8.0...1.9.0-alpha) (2018-05-10)

Expand Down
73 changes: 41 additions & 32 deletions src/cljs/snapshot/lumo/repl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

(def ^:private ^:dynamic *loading-foreign* false)
(def ^:private ^:dynamic *executing-path* nil)
(def ^:private ^:dynamic *source-name* nil)

(defonce ^:private st (cljs/empty-state))

Expand Down Expand Up @@ -359,13 +360,18 @@
"Evaluates JavaScript in node, writing source and analysis cache to disk
when desired."
[{:keys [name source cache path]}]
(when-let [cache-path (and source cache path (:cache-path @app-opts))]
(write-cache name path source cache cache-path))
(let [foreign? *loading-foreign*
exec-path *executing-path*]
(set! *loading-foreign* false)
(set! *executing-path* nil)
(js/$$LUMO_GLOBALS.eval source foreign? exec-path)))
(when-not (string/blank? source)
(when-let [cache-path (and source cache path (:cache-path @app-opts))]
(write-cache name path source cache cache-path))
(let [foreign? *loading-foreign*
exec-path *executing-path*]
(set! *loading-foreign* false)
(when (and (some? exec-path)
(some? *source-name*)
(or (symbol-identical? *source-name* name)
(identical? *source-name* name)))
(set! *executing-path* nil))
(js/$$LUMO_GLOBALS.eval source foreign? exec-path))))

;; =============================================================================
;; REPL plumbing
Expand Down Expand Up @@ -1044,7 +1050,7 @@
(println (form-indicator-str column @current-ns))))
(print warning-string)))))

(declare execute-source)
(declare execute-text)

(defn- execute-path [file opts]
(load {:file file}
Expand All @@ -1054,10 +1060,10 @@
cljs/*load-fn* load
*executing-path* file]
(condp keyword-identical? lang
:clj (execute-source source (merge opts
{:type "text"
:filename file
:expression? false}))
:clj (execute-text source (merge opts
{:type "text"
:filename file
:expression? false}))
:js (cljs/process-macros-deps {:*compiler* st} cache nil
(fn [{:keys [error]}]
(if-not (nil? error)
Expand Down Expand Up @@ -1090,29 +1096,32 @@
eval-opts (merge (make-eval-opts)
(when expression?
{:context :expr
:def-emits-var true}))]
:def-emits-var true}))
source-name (when (some? filename)
(or (ns-for-source source) filename))]
(if (repl-special? form)
((get repl-special-fns (first form)) form (merge opts eval-opts))
(cljs/eval-str
st
source
(cond
expression? source
filename (or (ns-for-source source) filename)
:else "source")
eval-opts
(fn [{:keys [ns value error] :as ret}]
(if-not error
(when expression?
(when (or (true? print-nil-result?)
(binding [*source-name* source-name]
(cljs/eval-str
st
source
(cond
expression? source
(some? source-name) source-name
:else "source")
eval-opts
(fn [{:keys [ns value error] :as ret}]
(if-not error
(when expression?
(when (or (true? print-nil-result?)
(not (nil? value)))
(js/$$LUMO_GLOBALS.doPrint print-value value))
(process-1-2-3 form value)
(when (def-form? form)
(let [{:keys [ns name]} (meta value)]
(swap! st assoc-in [::ana/namespaces ns :defs name ::repl-entered-source] source)))
(vreset! current-ns ns))
(handle-error error true)))))))
(js/$$LUMO_GLOBALS.doPrint print-value value))
(process-1-2-3 form value)
(when (def-form? form)
(let [{:keys [ns name]} (meta value)]
(swap! st assoc-in [::ana/namespaces ns :defs name ::repl-entered-source] source)))
(vreset! current-ns ns))
(handle-error error true))))))))
(catch :default e
;; `;;` and `#_`
(when-not (identical? (.-message e) "Unexpected EOF.")
Expand Down

0 comments on commit 658732f

Please sign in to comment.