Skip to content

Commit c7d84e0

Browse files
authored
Merge pull request #670 from bloomberg/add_play_ground_ready_to_switch
[docs][skip ci] add playground back to master branch ready to use mas…
2 parents 511de3c + 95fbfb9 commit c7d84e0

File tree

224 files changed

+220604
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

224 files changed

+220604
-2
lines changed

.gitattributes

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
jscomp/js_cmj_datasets.ml binary
22
jscomp/bin/compiler.ml binary
3-
jscomp/bin/reason.ml binary
3+
jscomp/bin/reason.ml binary
4+
docs/js-demo/exports.js binary
5+
docs/reason-demo/exports.js binary

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bsppx
4040
jscomp/tools/ocamlpack*
4141

4242
.idea
43-
exports.js
43+
4444
node_modules
4545
oxml
4646
test.json

docs/js-demo/.nojekyll

Whitespace-only changes.

docs/js-demo/build.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
"trigger", ".", {
3+
"name": "build",
4+
"expression": ["pcre", "\\.(js|html|css|ml)$"],
5+
"command": ["./build.sh"],
6+
"append_files" : true
7+
}
8+
]

docs/js-demo/build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
set -e
3+
reload codemirror

docs/js-demo/edit.js

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
'use strict';
2+
3+
var codeMirrorDefaultHeight = 10000;
4+
var myCode1Mirror = CodeMirror.fromTextArea(
5+
document.getElementById('ocamlcode#1'),
6+
{
7+
mode:'text/x-ocaml',
8+
lineNumbers: true,
9+
lineWrapping: true,
10+
styleActiveLine:true,
11+
theme: "monokai",
12+
matchBrackets: true,
13+
autoCloseBrackets: true
14+
} );
15+
var jsCode1Mirror = CodeMirror.fromTextArea(
16+
document.getElementById('jscode#1'),
17+
{ mode:'javascript',
18+
lineNumbers:true,
19+
readOnly: true,
20+
lineWrapping: true,
21+
theme: "monokai",
22+
});
23+
24+
var outputMirror = CodeMirror.fromTextArea(
25+
document.getElementById('output'),
26+
{
27+
mode : 'javascript',
28+
readOnly: true,
29+
lineWrapping: true,
30+
lineNumbers: true,
31+
theme: "monokai"
32+
}
33+
);
34+
var errorMirror = CodeMirror.fromTextArea(
35+
document.getElementById('error'),
36+
{
37+
readOnly: true,
38+
lineWrapping: true,
39+
lineNumbers : true,
40+
theme: "monokai"
41+
}
42+
);
43+
var PROMPT = "> " ;
44+
var log_output = PROMPT;
45+
var ERR_OUTPUT = "Warnings: "
46+
var err_output = ERR_OUTPUT;
47+
48+
function reset_log_output (){ log_output = PROMPT;}
49+
function reset_error_output(){ err_output = ERR_OUTPUT;}
50+
function get_log_output(){
51+
var old = log_output;
52+
reset_log_output();
53+
return old;
54+
}
55+
function get_error_output(){
56+
var old = err_output;
57+
reset_error_output();
58+
return old;
59+
}
60+
var compile_code ;
61+
var evalButton = document.getElementById('option-eval');
62+
var shakeButton = document.getElementById('option-non-export');
63+
64+
function shouldEval(){
65+
return evalButton.checked;
66+
}
67+
function onEvalButtonChange(){
68+
if(!shouldEval()){
69+
outputMirror.setValue(PROMPT);
70+
} else {
71+
onEditChanges();
72+
}
73+
}
74+
evalButton.addEventListener('change', onEvalButtonChange);
75+
76+
function onShakeButtonChange(){
77+
if(shakeButton.checked){
78+
compile_code = ocaml.shake_compile;
79+
}else{
80+
compile_code = ocaml.compile;
81+
}
82+
onEditChanges();
83+
}
84+
85+
shakeButton.addEventListener('change', onShakeButtonChange);
86+
var original_log = console.log;
87+
var original_err = console.error;
88+
89+
/**
90+
* TODO: simulate commonjs in browser
91+
* */
92+
var exports = window;
93+
94+
function redirect() { log_output = log_output + Array.prototype.slice.apply(arguments).join(' ') + "\n"};
95+
96+
function redirect_err() {
97+
err_output = err_output + Array.prototype.slice.apply(arguments).join(' ') + "\n"
98+
};
99+
100+
myCode1Mirror.setSize(null,codeMirrorDefaultHeight);
101+
outputMirror.setSize(null,50);
102+
outputMirror.setValue(PROMPT + 'Hello BuckleScript!');
103+
errorMirror.setSize(null,50);
104+
errorMirror.setValue(ERR_OUTPUT);
105+
106+
var sourceLocation = ""
107+
if (typeof window.location !== "undefined"){
108+
sourceLocation = "\n//# sourceURL=" + window.location.href + "/repl.js"
109+
}
110+
111+
function evalCode(js){
112+
console.log = redirect;
113+
try {
114+
window.eval(js + sourceLocation);
115+
outputMirror.setValue(get_log_output());
116+
console.log = original_log;
117+
}
118+
catch(e){
119+
outputMirror.setValue(get_log_output() + "\n" + e);
120+
console.log = original_log;
121+
}
122+
123+
}
124+
125+
function createExample(name){
126+
var li = document.createElement('li');
127+
var a = document.createElement('a')
128+
a.setAttribute('href', '#' + name);
129+
a.setAttribute('data-key', name);
130+
a.appendChild(document.createTextNode(name))
131+
li.appendChild(a)
132+
return li
133+
}
134+
135+
var examplesDropdown = document.getElementById("examplesDropdown")
136+
var examplesDataSet ;
137+
138+
139+
//Event handler for examples dropdown
140+
$('#examplesDropdown').click(clickHandler);
141+
142+
function switchExample(id){
143+
var filename = "";
144+
var example = examplesDataSet [id];
145+
if (example){
146+
changeEvalButton(example.eval)
147+
filename = "examples/" + example.file
148+
}
149+
//make ajax request
150+
$
151+
.ajax({
152+
url: filename,
153+
cache: true
154+
})
155+
.done(function (response) {
156+
myCode1Mirror.setValue(response);
157+
});
158+
159+
//update dropdown label
160+
$('#examplesLabel').html(id + ' <span class="caret"></span>');
161+
162+
}
163+
164+
function clickHandler(e) {
165+
var id = e.target.getAttribute('data-key');
166+
switchExample(id)
167+
}
168+
169+
170+
171+
function onEditChanges(cm, change) {
172+
if(typeof compile_code === 'undefined'){
173+
console.log('init....');
174+
compile_code = ocaml.compile;
175+
}
176+
console.error = redirect_err;
177+
var raw = compile_code(myCode1Mirror.getValue());
178+
errorMirror.setValue(get_error_output());
179+
console.error = original_err;
180+
console.log(raw);
181+
var rsp = JSON.parse(raw); // can we save this from parsing?
182+
if (rsp.js_code !== undefined) {
183+
jsCode1Mirror.setValue(rsp.js_code);
184+
// eval
185+
if(shouldEval()) {
186+
evalCode(rsp.js_code)
187+
}
188+
} else {
189+
jsCode1Mirror.setValue(rsp.js_error_msg);
190+
191+
}
192+
193+
}
194+
myCode1Mirror.on("changes", onEditChanges);
195+
196+
jsCode1Mirror.setSize(null,codeMirrorDefaultHeight);
197+
198+
199+
200+
//checks or unchecks the eval button
201+
function changeEvalButton(bool) {
202+
$('#option-eval').prop('checked', bool);
203+
onEvalButtonChange();
204+
}
205+
206+
//creates a gist from OCaml code
207+
$('#share').click(function (e) {
208+
var state = $(this).button('loading');
209+
var request =
210+
{
211+
"description": "BuckleScript Gist",
212+
"public": true,
213+
"files": {
214+
"gist.ml": {
215+
"content": myCode1Mirror.getValue()
216+
}
217+
}
218+
};
219+
220+
$
221+
.ajax({ url:'https://api.github.com/gists',
222+
type: 'POST',
223+
data: JSON.stringify(request)
224+
})
225+
.done(function (response) {
226+
state.button('reset');
227+
$('#shareModal').modal('show');
228+
var url = 'https://bloomberg.github.io/bucklescript/js-demo/?gist=' + response.id;
229+
$('#shareModalBody').html('<a href=' + '"' + url + '"' + 'target="_blank"' + '>' + url + '</a>');
230+
})
231+
.error(function (err) {
232+
state.button('reset');
233+
$('#shareModal').modal('show');
234+
$('#shareModalBody').text('Sorry! Currently GitHub\'s API limits the number of requests we can send per hour. Please try again later.');
235+
})
236+
});
237+
238+
//copy link to clipboard
239+
var copy = new Clipboard('#copyButton');
240+
copy.on('success', function(e) {
241+
e.clearSelection();
242+
$('#copyGlyph').attr('class', 'glyphicon glyphicon-ok');
243+
});
244+
245+
//reset clipboard icon when modal is closed
246+
$('#shareModal').on('hidden.bs.modal', function (e) {
247+
$('#copyGlyph').attr('class', 'glyphicon glyphicon-copy');
248+
});

docs/js-demo/examples/curry.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(** Test curry and uncurry calling convention *)
2+
let test_curry x y = x + y
3+
let f = test_curry 32

docs/js-demo/examples/default.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
Js.log "Hello BuckleScript!"
3+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(* node.js readline class *)
2+
type readline
3+
4+
(* bindings to event handler for 'close' and 'line' events *)
5+
external on : readline ->
6+
([`close of unit -> unit
7+
| `line of string -> unit] [@bs.string])
8+
-> unit = "" [@@bs.module "readline"]
9+
10+
(* register event handlers *)
11+
let register rl =
12+
on rl (`close (fun event -> () ));
13+
on rl (`line (fun line -> print_endline line));

docs/js-demo/examples/examples.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"Default Example" :
3+
{ "file" : "default.ml",
4+
"eval" : true
5+
},
6+
"Event Handler" :
7+
{ "file" : "event_handler.ml",
8+
"eval" : false},
9+
"Quick Sort":
10+
{"file" : "quick_sort.ml",
11+
"eval" : true
12+
},
13+
"Tail Call" :
14+
{
15+
"file" : "tailcall.ml",
16+
"eval" : true
17+
},
18+
"Functional Programming" :
19+
{
20+
"file" : "fp.ml",
21+
"eval" : true
22+
},
23+
"Curry Optimization" :
24+
{
25+
"file" : "curry.ml",
26+
"eval" : true
27+
},
28+
"For Loop" : {
29+
"file" : "for_loop.ml",
30+
"eval" : true
31+
},
32+
"Use Standard Library" : {
33+
"file" : "use_stdlib.ml",
34+
"eval" : true
35+
},
36+
"JS Objects" : {
37+
"file" : "js_object.ml",
38+
"eval" : true
39+
},
40+
"Left Pad" : {
41+
"file" : "left_pad.ml",
42+
"eval" : true
43+
}
44+
}

0 commit comments

Comments
 (0)