Skip to content

Commit

Permalink
implement runtime api for requesting ad-hoc code evaluation (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed Sep 17, 2016
1 parent b5b3ba7 commit 37ec8ba
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
23 changes: 21 additions & 2 deletions resources/unpacked/devtools/front_end/console/ConsoleView.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,33 @@ WebInspector.ConsoleView.prototype = {
return this._prompt.text();
},

_onConsoleDiracMessage: function(event)
{
handleEvalCLJSConsoleDiracMessage: function(message) {
const code = message.parameters[2];
if (code && goog.isString(code.value)) {
this.appendDiracCommand(code.value, null);
}
},

handleEvalJSConsoleDiracMessage: function(message) {
const code = message.parameters[2];
if (code && goog.isString(code.value)) {
this._appendCommand(code.value, true);
}
},

_onConsoleDiracMessage: function(event) {
var message = (event.data);
var command = message.parameters[1];
if (command)
command = command.value;

switch (command) {
case "eval-cljs":
this.handleEvalCLJSConsoleDiracMessage(message);
break;
case "eval-js":
this.handleEvalJSConsoleDiracMessage(message);
break;
default:
throw ("unrecognized Dirac message: " + command);
}
Expand Down
2 changes: 1 addition & 1 deletion src/implant/dirac/implant/intercom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[goog.functions :as gfns])
(:import goog.net.WebSocket.ErrorEvent))

(defonce required-repl-api-version 3)
(defonce required-repl-api-version 4)

(defonce ^:dynamic *debugger-events-subscribed* false)
(defonce ^:dynamic *repl-connected* false)
Expand Down
10 changes: 9 additions & 1 deletion src/runtime/dirac/runtime/repl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@

; -- REPL API ---------------------------------------------------------------------------------------------------------------

(def api-version 3) ; version of REPL API
(def api-version 4) ; version of REPL API

(defn ^:export get-api-version []
api-version)
Expand Down Expand Up @@ -161,6 +161,14 @@
(.-stack ex)
"No stacktrace available.")}))

(defn ^:export request-eval-cljs [code]
(assert (string? code) "Code passed for evaluation must be a string")
(call-dirac "eval-cljs" code))

(defn ^:export request-eval-js [code]
(assert (string? code) "Code passed for evaluation must be a string")
(call-dirac "eval-js" code))

; -- install/uninstall ------------------------------------------------------------------------------------------------------

(defn installed? []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
(init-runtime!)
(scenario/register-trigger! :reload #(js/window.location.reload))
(scenario/register-trigger! :navigate #(set! js/window.location.pathname %))
(scenario/register-trigger! :eval-cljs #(js/dirac.runtime.repl.request-eval-cljs %))
(scenario/register-trigger! :eval-js #(js/dirac.runtime.repl.request-eval-js %))
(scenario/ready!)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
(<!* a/wait-for-devtools-match "<elided stack trace log>")
(<!* a/wait-for-devtools-match #"^JS.log")
(<!* a/console-exec-and-match! "(in-ns 'my.ns)" "setDiracPromptNS('my.ns')"))))
(testing "page-initiated eval requests, https://github.com/binaryage/dirac/issues/38"
(with-devtools
(<!* a/switch-to-console-panel!)
(<!* a/switch-prompt-to-dirac!)
(<!* a/wait-for-prompt-to-enter-edit-mode)
(with-console-feedback
(<!* a/trigger! :eval-js "console.log('js code here'); 1+3")
(<!* a/wait-for-devtools-match "js code here")
(<!* a/wait-for-devtools-match "JS.log> 4")
(<!* a/trigger! :eval-cljs "(+ 2 40)")
(<!* a/wait-for-devtools-match "DF.log> 42")
(<!* a/wait-for-devtools-match "repl eval job ended"))))
#_(testing "page refresh while REPL was connected"
(with-devtools
(<!* a/switch-to-console-panel!)
Expand Down

0 comments on commit 37ec8ba

Please sign in to comment.