| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
function HTTPTransport(enableVet) { |
| 47 |
'use strict'; |
| 48 |
|
| 49 |
function playback(output, data) { |
| 50 |
|
| 51 |
var events = data.Events || []; |
| 52 |
var errors = data.Errors || ''; |
| 53 |
var status = data.Status || 0; |
| 54 |
var isTest = data.IsTest || false; |
| 55 |
var testsFailed = data.TestsFailed || 0; |
| 56 |
|
| 57 |
var timeout; |
| 58 |
output({ Kind: 'start' }); |
| 59 |
function next() { |
| 60 |
if (!events || events.length === 0) { |
| 61 |
if (isTest) { |
| 62 |
if (testsFailed > 0) { |
| 63 |
output({ |
| 64 |
Kind: 'system', |
| 65 |
Body: |
| 66 |
'\n' + |
| 67 |
testsFailed + |
| 68 |
' test' + |
| 69 |
(testsFailed > 1 ? 's' : '') + |
| 70 |
' failed.', |
| 71 |
}); |
| 72 |
} else { |
| 73 |
output({ Kind: 'system', Body: '\nAll tests passed.' }); |
| 74 |
} |
| 75 |
} else { |
| 76 |
if (status > 0) { |
| 77 |
output({ Kind: 'end', Body: 'status ' + status + '.' }); |
| 78 |
} else { |
| 79 |
if (errors !== '') { |
| 80 |
|
| 81 |
output({ Kind: 'end', Body: errors + '.' }); |
| 82 |
} else { |
| 83 |
output({ Kind: 'end' }); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
return; |
| 88 |
} |
| 89 |
var e = events.shift(); |
| 90 |
if (e.Delay === 0) { |
| 91 |
output({ Kind: e.Kind, Body: e.Message }); |
| 92 |
next(); |
| 93 |
return; |
| 94 |
} |
| 95 |
timeout = setTimeout(function() { |
| 96 |
output({ Kind: e.Kind, Body: e.Message }); |
| 97 |
next(); |
| 98 |
}, e.Delay / 1000000); |
| 99 |
} |
| 100 |
next(); |
| 101 |
return { |
| 102 |
Stop: function() { |
| 103 |
clearTimeout(timeout); |
| 104 |
}, |
| 105 |
}; |
| 106 |
} |
| 107 |
|
| 108 |
function error(output, msg) { |
| 109 |
output({ Kind: 'start' }); |
| 110 |
output({ Kind: 'stderr', Body: msg }); |
| 111 |
output({ Kind: 'end' }); |
| 112 |
} |
| 113 |
|
| 114 |
function buildFailed(output, msg) { |
| 115 |
output({ Kind: 'start' }); |
| 116 |
output({ Kind: 'stderr', Body: msg }); |
| 117 |
output({ Kind: 'system', Body: '\nGo build failed.' }); |
| 118 |
} |
| 119 |
|
| 120 |
var seq = 0; |
| 121 |
return { |
| 122 |
Run: function(body, output, options) { |
| 123 |
seq++; |
| 124 |
var cur = seq; |
| 125 |
var playing; |
| 126 |
$.ajax('/compile', { |
| 127 |
type: 'POST', |
| 128 |
data: { version: 2, body: body, withVet: enableVet }, |
| 129 |
dataType: 'json', |
| 130 |
success: function(data) { |
| 131 |
if (seq != cur) return; |
| 132 |
if (!data) return; |
| 133 |
if (playing != null) playing.Stop(); |
| 134 |
if (data.Errors) { |
| 135 |
if (data.Errors === 'process took too long') { |
| 136 |
|
| 137 |
playing = playback(output, data); |
| 138 |
} else { |
| 139 |
buildFailed(output, data.Errors); |
| 140 |
} |
| 141 |
return; |
| 142 |
} |
| 143 |
if (!data.Events) { |
| 144 |
data.Events = []; |
| 145 |
} |
| 146 |
if (data.VetErrors) { |
| 147 |
|
| 148 |
data.Events.unshift({ |
| 149 |
Message: 'Go vet exited.\n\n', |
| 150 |
Kind: 'system', |
| 151 |
Delay: 0, |
| 152 |
}); |
| 153 |
data.Events.unshift({ |
| 154 |
Message: data.VetErrors, |
| 155 |
Kind: 'stderr', |
| 156 |
Delay: 0, |
| 157 |
}); |
| 158 |
} |
| 159 |
|
| 160 |
if (!enableVet || data.VetOK || data.VetErrors) { |
| 161 |
playing = playback(output, data); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
$.ajax('/vet', { |
| 171 |
data: { body: body }, |
| 172 |
type: 'POST', |
| 173 |
dataType: 'json', |
| 174 |
success: function(dataVet) { |
| 175 |
if (dataVet.Errors) { |
| 176 |
|
| 177 |
data.Events.unshift({ |
| 178 |
Message: 'Go vet exited.\n\n', |
| 179 |
Kind: 'system', |
| 180 |
Delay: 0, |
| 181 |
}); |
| 182 |
data.Events.unshift({ |
| 183 |
Message: dataVet.Errors, |
| 184 |
Kind: 'stderr', |
| 185 |
Delay: 0, |
| 186 |
}); |
| 187 |
} |
| 188 |
playing = playback(output, data); |
| 189 |
}, |
| 190 |
error: function() { |
| 191 |
playing = playback(output, data); |
| 192 |
}, |
| 193 |
}); |
| 194 |
}, |
| 195 |
error: function() { |
| 196 |
error(output, 'Error communicating with remote server.'); |
| 197 |
}, |
| 198 |
}); |
| 199 |
return { |
| 200 |
Kill: function() { |
| 201 |
if (playing != null) playing.Stop(); |
| 202 |
output({ Kind: 'end', Body: 'killed' }); |
| 203 |
}, |
| 204 |
}; |
| 205 |
}, |
| 206 |
}; |
| 207 |
} |
| 208 |
|
| 209 |
function SocketTransport() { |
| 210 |
'use strict'; |
| 211 |
|
| 212 |
var id = 0; |
| 213 |
var outputs = {}; |
| 214 |
var started = {}; |
| 215 |
var websocket; |
| 216 |
if (window.location.protocol == 'http:') { |
| 217 |
websocket = new WebSocket('ws://' + window.location.host + '/socket'); |
| 218 |
} else if (window.location.protocol == 'https:') { |
| 219 |
websocket = new WebSocket('wss://' + window.location.host + '/socket'); |
| 220 |
} |
| 221 |
|
| 222 |
websocket.onclose = function() { |
| 223 |
console.log('websocket connection closed'); |
| 224 |
}; |
| 225 |
|
| 226 |
websocket.onmessage = function(e) { |
| 227 |
var m = JSON.parse(e.data); |
| 228 |
var output = outputs[m.Id]; |
| 229 |
if (output === null) return; |
| 230 |
if (!started[m.Id]) { |
| 231 |
output({ Kind: 'start' }); |
| 232 |
started[m.Id] = true; |
| 233 |
} |
| 234 |
output({ Kind: m.Kind, Body: m.Body }); |
| 235 |
}; |
| 236 |
|
| 237 |
function send(m) { |
| 238 |
websocket.send(JSON.stringify(m)); |
| 239 |
} |
| 240 |
|
| 241 |
return { |
| 242 |
Run: function(body, output, options) { |
| 243 |
var thisID = id + ''; |
| 244 |
id++; |
| 245 |
outputs[thisID] = output; |
| 246 |
send({ Id: thisID, Kind: 'run', Body: body, Options: options }); |
| 247 |
return { |
| 248 |
Kill: function() { |
| 249 |
send({ Id: thisID, Kind: 'kill' }); |
| 250 |
}, |
| 251 |
}; |
| 252 |
}, |
| 253 |
}; |
| 254 |
} |
| 255 |
|
| 256 |
function PlaygroundOutput(el) { |
| 257 |
'use strict'; |
| 258 |
|
| 259 |
return function(write) { |
| 260 |
if (write.Kind == 'start') { |
| 261 |
el.innerHTML = ''; |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
var cl = 'system'; |
| 266 |
if (write.Kind == 'stdout' || write.Kind == 'stderr') cl = write.Kind; |
| 267 |
|
| 268 |
var m = write.Body; |
| 269 |
if (write.Kind == 'end') { |
| 270 |
m = '\nProgram exited' + (m ? ': ' + m : '.'); |
| 271 |
} |
| 272 |
|
| 273 |
if (m.indexOf('IMAGE:') === 0) { |
| 274 |
|
| 275 |
var url = 'data:image/png;base64,' + m.substr(6); |
| 276 |
var img = document.createElement('img'); |
| 277 |
img.src = url; |
| 278 |
el.appendChild(img); |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
var s = m.split('\x0c'); |
| 284 |
if (s.length > 1) { |
| 285 |
el.innerHTML = ''; |
| 286 |
m = s.pop(); |
| 287 |
} |
| 288 |
|
| 289 |
m = m.replace(/&/g, '&'); |
| 290 |
m = m.replace(/</g, '<'); |
| 291 |
m = m.replace(/>/g, '>'); |
| 292 |
|
| 293 |
var needScroll = el.scrollTop + el.offsetHeight == el.scrollHeight; |
| 294 |
|
| 295 |
var span = document.createElement('span'); |
| 296 |
span.className = cl; |
| 297 |
span.innerHTML = m; |
| 298 |
el.appendChild(span); |
| 299 |
|
| 300 |
if (needScroll) el.scrollTop = el.scrollHeight - el.offsetHeight; |
| 301 |
}; |
| 302 |
} |
| 303 |
|
| 304 |
(function() { |
| 305 |
function lineHighlight(error) { |
| 306 |
var regex = /prog.go:([0-9]+)/g; |
| 307 |
var r = regex.exec(error); |
| 308 |
while (r) { |
| 309 |
$('.lines div') |
| 310 |
.eq(r[1] - 1) |
| 311 |
.addClass('lineerror'); |
| 312 |
r = regex.exec(error); |
| 313 |
} |
| 314 |
} |
| 315 |
function highlightOutput(wrappedOutput) { |
| 316 |
return function(write) { |
| 317 |
if (write.Body) lineHighlight(write.Body); |
| 318 |
wrappedOutput(write); |
| 319 |
}; |
| 320 |
} |
| 321 |
function lineClear() { |
| 322 |
$('.lineerror').removeClass('lineerror'); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
function playground(opts) { |
| 340 |
var code = $(opts.codeEl); |
| 341 |
var transport = opts['transport'] || new HTTPTransport(opts['enableVet']); |
| 342 |
var running; |
| 343 |
|
| 344 |
|
| 345 |
function insertTabs(n) { |
| 346 |
|
| 347 |
var start = code[0].selectionStart; |
| 348 |
var end = code[0].selectionEnd; |
| 349 |
|
| 350 |
var v = code[0].value; |
| 351 |
var u = v.substr(0, start); |
| 352 |
for (var i = 0; i < n; i++) { |
| 353 |
u += '\t'; |
| 354 |
} |
| 355 |
u += v.substr(end); |
| 356 |
|
| 357 |
code[0].value = u; |
| 358 |
|
| 359 |
code[0].selectionStart = start + n; |
| 360 |
code[0].selectionEnd = start + n; |
| 361 |
} |
| 362 |
function autoindent(el) { |
| 363 |
var curpos = el.selectionStart; |
| 364 |
var tabs = 0; |
| 365 |
while (curpos > 0) { |
| 366 |
curpos--; |
| 367 |
if (el.value[curpos] == '\t') { |
| 368 |
tabs++; |
| 369 |
} else if (tabs > 0 || el.value[curpos] == '\n') { |
| 370 |
break; |
| 371 |
} |
| 372 |
} |
| 373 |
setTimeout(function() { |
| 374 |
insertTabs(tabs); |
| 375 |
}, 1); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
function handleSaveShortcut(e) { |
| 380 |
if (e.isDefaultPrevented()) return false; |
| 381 |
if (!e.metaKey && !e.ctrlKey) return false; |
| 382 |
if (e.key != 'S' && e.key != 's') return false; |
| 383 |
|
| 384 |
e.preventDefault(); |
| 385 |
|
| 386 |
|
| 387 |
share(function(url) { |
| 388 |
window.location.href = url + '.go?download=true'; |
| 389 |
}); |
| 390 |
|
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
function keyHandler(e) { |
| 395 |
if (opts.enableShortcuts && handleSaveShortcut(e)) return; |
| 396 |
|
| 397 |
if (e.keyCode == 9 && !e.ctrlKey) { |
| 398 |
|
| 399 |
insertTabs(1); |
| 400 |
e.preventDefault(); |
| 401 |
return false; |
| 402 |
} |
| 403 |
if (e.keyCode == 13) { |
| 404 |
|
| 405 |
if (e.shiftKey) { |
| 406 |
|
| 407 |
run(); |
| 408 |
e.preventDefault(); |
| 409 |
return false; |
| 410 |
} |
| 411 |
if (e.ctrlKey) { |
| 412 |
|
| 413 |
fmt(); |
| 414 |
e.preventDefault(); |
| 415 |
} else { |
| 416 |
autoindent(e.target); |
| 417 |
} |
| 418 |
} |
| 419 |
return true; |
| 420 |
} |
| 421 |
code.unbind('keydown').bind('keydown', keyHandler); |
| 422 |
var outdiv = $(opts.outputEl).empty(); |
| 423 |
var output = $('<pre/>').appendTo(outdiv); |
| 424 |
|
| 425 |
function body() { |
| 426 |
return $(opts.codeEl).val(); |
| 427 |
} |
| 428 |
function setBody(text) { |
| 429 |
$(opts.codeEl).val(text); |
| 430 |
} |
| 431 |
function origin(href) { |
| 432 |
return ('' + href) |
| 433 |
.split('/') |
| 434 |
.slice(0, 3) |
| 435 |
.join('/'); |
| 436 |
} |
| 437 |
|
| 438 |
var pushedEmpty = window.location.pathname == '/'; |
| 439 |
function inputChanged() { |
| 440 |
if (pushedEmpty) { |
| 441 |
return; |
| 442 |
} |
| 443 |
pushedEmpty = true; |
| 444 |
$(opts.shareURLEl).hide(); |
| 445 |
window.history.pushState(null, '', '/'); |
| 446 |
} |
| 447 |
function popState(e) { |
| 448 |
if (e === null) { |
| 449 |
return; |
| 450 |
} |
| 451 |
if (e && e.state && e.state.code) { |
| 452 |
setBody(e.state.code); |
| 453 |
} |
| 454 |
} |
| 455 |
var rewriteHistory = false; |
| 456 |
if ( |
| 457 |
window.history && |
| 458 |
window.history.pushState && |
| 459 |
window.addEventListener && |
| 460 |
opts.enableHistory |
| 461 |
) { |
| 462 |
rewriteHistory = true; |
| 463 |
code[0].addEventListener('input', inputChanged); |
| 464 |
window.addEventListener('popstate', popState); |
| 465 |
} |
| 466 |
|
| 467 |
function setError(error) { |
| 468 |
if (running) running.Kill(); |
| 469 |
lineClear(); |
| 470 |
lineHighlight(error); |
| 471 |
output |
| 472 |
.empty() |
| 473 |
.addClass('error') |
| 474 |
.text(error); |
| 475 |
} |
| 476 |
function loading() { |
| 477 |
lineClear(); |
| 478 |
if (running) running.Kill(); |
| 479 |
output.removeClass('error').text('Waiting for remote server...'); |
| 480 |
} |
| 481 |
function run() { |
| 482 |
loading(); |
| 483 |
running = transport.Run( |
| 484 |
body(), |
| 485 |
highlightOutput(PlaygroundOutput(output[0])) |
| 486 |
); |
| 487 |
} |
| 488 |
|
| 489 |
function fmt() { |
| 490 |
loading(); |
| 491 |
var data = { body: body() }; |
| 492 |
if ($(opts.fmtImportEl).is(':checked')) { |
| 493 |
data['imports'] = 'true'; |
| 494 |
} |
| 495 |
$.ajax('/fmt', { |
| 496 |
data: data, |
| 497 |
type: 'POST', |
| 498 |
dataType: 'json', |
| 499 |
success: function(data) { |
| 500 |
if (data.Error) { |
| 501 |
setError(data.Error); |
| 502 |
} else { |
| 503 |
setBody(data.Body); |
| 504 |
setError(''); |
| 505 |
} |
| 506 |
}, |
| 507 |
}); |
| 508 |
} |
| 509 |
|
| 510 |
var shareURL; |
| 511 |
var sharing = false; |
| 512 |
var shareCallbacks = []; |
| 513 |
function share(opt_callback) { |
| 514 |
if (opt_callback) shareCallbacks.push(opt_callback); |
| 515 |
|
| 516 |
if (sharing) return; |
| 517 |
sharing = true; |
| 518 |
|
| 519 |
var sharingData = body(); |
| 520 |
$.ajax('https://play.golang.org/share', { |
| 521 |
processData: false, |
| 522 |
data: sharingData, |
| 523 |
type: 'POST', |
| 524 |
contentType: 'text/plain; charset=utf-8', |
| 525 |
complete: function(xhr) { |
| 526 |
sharing = false; |
| 527 |
if (xhr.status != 200) { |
| 528 |
alert('Server error; try again.'); |
| 529 |
return; |
| 530 |
} |
| 531 |
if (opts.shareRedirect) { |
| 532 |
window.location = opts.shareRedirect + xhr.responseText; |
| 533 |
} |
| 534 |
var path = '/p/' + xhr.responseText; |
| 535 |
var url = origin(window.location) + path; |
| 536 |
|
| 537 |
for (var i = 0; i < shareCallbacks.length; i++) { |
| 538 |
shareCallbacks[i](url); |
| 539 |
} |
| 540 |
shareCallbacks = []; |
| 541 |
|
| 542 |
if (shareURL) { |
| 543 |
shareURL |
| 544 |
.show() |
| 545 |
.val(url) |
| 546 |
.focus() |
| 547 |
.select(); |
| 548 |
|
| 549 |
if (rewriteHistory) { |
| 550 |
var historyData = { code: sharingData }; |
| 551 |
window.history.pushState(historyData, '', path); |
| 552 |
pushedEmpty = false; |
| 553 |
} |
| 554 |
} |
| 555 |
}, |
| 556 |
}); |
| 557 |
} |
| 558 |
|
| 559 |
$(opts.runEl).click(run); |
| 560 |
$(opts.fmtEl).click(fmt); |
| 561 |
|
| 562 |
if ( |
| 563 |
opts.shareEl !== null && |
| 564 |
(opts.shareURLEl !== null || opts.shareRedirect !== null) |
| 565 |
) { |
| 566 |
if (opts.shareURLEl) { |
| 567 |
shareURL = $(opts.shareURLEl).hide(); |
| 568 |
} |
| 569 |
$(opts.shareEl).click(function() { |
| 570 |
share(); |
| 571 |
}); |
| 572 |
} |
| 573 |
|
| 574 |
if (opts.toysEl !== null) { |
| 575 |
$(opts.toysEl).bind('change', function() { |
| 576 |
var toy = $(this).val(); |
| 577 |
$.ajax('/doc/play/' + toy, { |
| 578 |
processData: false, |
| 579 |
type: 'GET', |
| 580 |
complete: function(xhr) { |
| 581 |
if (xhr.status != 200) { |
| 582 |
alert('Server error; try again.'); |
| 583 |
return; |
| 584 |
} |
| 585 |
setBody(xhr.responseText); |
| 586 |
}, |
| 587 |
}); |
| 588 |
}); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
window.playground = playground; |
| 593 |
})(); |