Skip to content

Rachitnigam master #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions resources/public/stopify-dbg.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>KLIPSE: a simple and elegant online cljs compiler and evaluator</title>
<link rel='shortcut icon' type='image/x-icon' href='img/klipse.png' />
<link rel="stylesheet" type="text/css" href="css/codemirror.css">
</head>
<body>
<style>
.klipse-snippet {
width: 50%;
float: left;
}
.klipse-result {
width: 50%;
float:left;
}
.klipse-separator {
clear: both;
}
</style>

<pre><code class="js" data-async-code="true">
function a () { return "a";}
window.a = a;
setTimeout(function() {
console.log("1st snippet", 1/0, [1, 2, {a: 2}]);
}, 1000);
</code></pre>
<pre><code class="js" data-async-code="true">
setTimeout(function() {
console.log("1st snippet", 1/0, [1, 2, {a: a()}]);
}, 1000);
</code></pre>

<pre><code class="js" data-async-code="true">
setTimeout(function() {
console.log("2nd snippet");
console.log("2nd snippet - bb");
}, 500);
</code></pre>


<pre><code class="js">
2 + 3
</code></pre>

<pre><code class="js">
console.log("inside");
</code></pre>
<pre><code class="js">
a = new Set();
a.add(1);
a.add(2);
x = {a: 1,
aa: a,
date: new Date(),
rrr: /^aa[a]+$/,
c: [1/0, NaN, Promise.resolve(1)],
arr: new Int32Array(3),
d: Symbol("dd")}
x.selfRef = x;
x
</code></pre>
<br/>
<pre><code class="js" data-gist-id="viebel/5cc67a97903f04036b569c0eb0436e5f">
</code></pre>

<pre><code class="js">
permutations([1, 2, 3])
</code></pre>

<div class="eval-js">
console.log("CCC")
2 + 3
</div>
<br/>

<div class="eval-js">
klipse_container.innerHTML="hello";
2+1
</div>
<br/>

<div class="eval-js">
klipse_container.innerHTML="foo";
[1,2,3]
</div>
<br/>

<div class="eval-js">
throw 'This is an exception.'
</div>

<h2> Function defs should be shared between snippets </h2>
<pre><code class="eval-js">
function foo(){
if (5 > 2){
return 5;
} else {
return 9;
}
}
</code></pre>
<pre><code class="eval-js">
foo()
</code></pre>
<div class="eval-js">
console.log("aa")
console.log("aa", 1, 2, 34, 4)
console.log("bb")
2 + 3
</div>
<br/>

<script>
window.klipse_settings = {
selector_eval_js_stopify: '.js,.eval-js',
};
</script>
<script src="/cljs-out/dev-main.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/klipse/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(enable-console-print!)
(gadjett/settings! :max-function-calls 100)

(def version "7.10.6")
(def version "7.11.0")
(js/console.info "Klipse " version)
(js/console.info "Clojurescript " *clojurescript-version*)

80 changes: 67 additions & 13 deletions src/klipse/lang/javascript.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
[klipse.common.registry :refer [codemirror-mode-src scripts-src register-mode]]
[applied-science.js-interop :as j]))

;(set! *warn-on-infer* true)
;(set! *warn-on-infer* true)
(def known-external-libs
{
"immutable" "https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"
"jQuery" "https://code.jquery.com/jquery-2.2.4.js"
"immutable" "https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"
"jQuery" "https://code.jquery.com/jquery-2.2.4.js"
"underscore" "http://underscorejs.org/underscore-min.js"})

(defn external-lib-path [lib-name-or-url]
Expand All @@ -39,7 +39,7 @@
Returns the empty string.
It works fine also with asynchronous code."
[c exp]
(let [logger (append-to-chan c)
(let [logger (append-to-chan c)
wrapped-exp (str "(function(console) {" exp "}(window.klipse_snippet_console))")]
(set! js/klipse_snippet_console #js {:log logger})
(eval-in-global-scope wrapped-exp)
Expand Down Expand Up @@ -69,15 +69,69 @@
c))


(def opts {:editor-in-mode "javascript"
:editor-out-mode "javascript"
(def opts {:editor-in-mode "javascript"
:editor-out-mode "javascript"
:beautify-output? false
:eval-fn str-eval-js-async
:external-scripts [(codemirror-mode-src "javascript") (scripts-src "pretty_format.js")]
:comment-str "//"})
:eval-fn str-eval-js-async
:external-scripts [(codemirror-mode-src "javascript") (scripts-src "pretty_format.js")]
:comment-str "//"})

(register-mode "eval-javascript" "selector_eval_js" opts)

(defn stopify-compile [source]
(let [asyncRun (j/call js/stopify :stopifyLocally source)]
;; Set the function called on the last expression
(aset asyncRun.g "callbackLast" (j/get js/console :log))
asyncRun))

;; Stopify runtime captures exceptions. The callback handles them correctly.
(defn stopify-cb [result]
(if (= (aget result "type") "exception")
(j/call js/console :log "Exception: " (aget result "value"))
js/undefined))

(defn stopify-run [asyncRun]
(j/call js/console :info asyncRun.code)
(j/call asyncRun :run stopify-cb)
"")

(defn str-eval-js-stopify-async [exp {:keys [async-code? external-libs container-id] :or {async-code? false external-libs nil}}]
(let [c (chan)]
(when (verbose?) (js/console.info "[javascript] evaluating" exp))
(go
(if (string/blank? exp)
(put! c "")
(do (setup-container! container-id)
(let [[status http-status script]
(<! (load-scripts
(map external-lib-path external-libs)
:secured-eval? false))]
(try
(put! c (if (= :ok status)
(try
(if async-code?
(eval-with-logger! c exp)
(my-with-redefs [js/console.log (append-to-chan c)]
(-> exp
stopify-compile
stopify-run)))
(catch :default o
(str o)))
(str "//Cannot load script: " script "\n"
"//Error: " http-status))))))))
c))

(def stopify-opts {:editor-in-mode "javascript"
:editor-out-mode "javascript"
:beautify-output? false
:eval-fn str-eval-js-stopify-async
:external-scripts [(codemirror-mode-src "javascript")
(scripts-src "stopify-full.bundle.js")
(scripts-src "pretty_format.js")]
:comment-str "//"})

(register-mode "eval-javascript-stopify" "selector_eval_js_stopify" stopify-opts)

(defn babel [src]
(-> (j/call js/Babel :transform src #js {:presets #js ["es2017" "stage-2" "stage-3"]})
(aget "code")))
Expand All @@ -101,11 +155,11 @@
(put! c (str o))))
c))

(def es2017-opts {:editor-in-mode "javascript"
:editor-out-mode "javascript"
(def es2017-opts {:editor-in-mode "javascript"
:editor-out-mode "javascript"
:beautify-output? false
:eval-fn eval-es2017
:eval-fn eval-es2017
:external-scripts [(codemirror-mode-src "javascript") (scripts-src "pretty_format.js") (scripts-src "babel.min.js") (scripts-src "babel_polyfill.min.js")]
:comment-str "//"})
:comment-str "//"})

(register-mode "eval-es2017" "selector_es2017" es2017-opts)