Update error handling

This commit is contained in:
titaneric 2021-04-13 01:08:24 +08:00
parent b206ed0689
commit f49d0cc17e
1 changed files with 28 additions and 16 deletions

View File

@ -60,7 +60,6 @@ function playground_text(playground) {
mac: "Ctrl-Enter" mac: "Ctrl-Enter"
}, },
exec: _editor => { exec: _editor => {
console.log(code_block.classList.contains("wasm"));
if (code_block.classList.contains("wasm")) { if (code_block.classList.contains("wasm")) {
run_wasm_pack_code(playground_block); run_wasm_pack_code(playground_block);
} else { } else {
@ -161,18 +160,16 @@ function playground_text(playground) {
result_block.innerText = "Running..."; result_block.innerText = "Running...";
prepareSandbox(params).then(src => processHTML(src)).then(html => { prepareSandbox(params)
result_block.innerText = ""; .then(src => processHTML(src))
var iframe = document.createElement('iframe'); .then(html => {
iframe.style.height = "100%"; result_block.innerText = "";
iframe.style.width = "100%"; var iframe = createIFrame(html);
iframe.style.padding = 0; result_block.appendChild(iframe);
iframe.style.margin = 0; })
iframe.style.border = 0; .catch(error => result_block.innerText = "Playground Communication: " + error.message);
iframe.src = createObjectURL(html, "text/html");
result_block.appendChild(iframe);
});
} }
// Greatly inspired from WebAssemblyStudio // Greatly inspired from WebAssemblyStudio
async function prepareSandbox(params) { async function prepareSandbox(params) {
var wasmResult = fetch_with_timeout("http://127.0.0.1:9999/wasm-pack", { var wasmResult = fetch_with_timeout("http://127.0.0.1:9999/wasm-pack", {
@ -184,13 +181,16 @@ function playground_text(playground) {
body: JSON.stringify(params) body: JSON.stringify(params)
}) })
.then(response => response.json()) .then(response => response.json())
.then(({ wasm_js, wasm_bg }) => { .then(({ wasm_js, wasm_bg, error }) => {
if (error) {
throw new Error(error);
}
return { return {
wasm_js: atob(wasm_js), wasm_js: atob(wasm_js),
wasm_bg: base64ToByteArray(wasm_bg) wasm_bg: base64ToByteArray(wasm_bg)
} }
}) })
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
var htmlSrc = fetch(new Request("iframe.html")) var htmlSrc = fetch(new Request("iframe.html"))
.then(response => response.text()); .then(response => response.text());
@ -198,8 +198,8 @@ function playground_text(playground) {
.then(response => response.text()); .then(response => response.text());
return Promise.all([htmlSrc, jsSrc, wasmResult]) return Promise.all([htmlSrc, jsSrc, wasmResult])
.catch(error => console.log(error));
} }
function base64ToByteArray(src) { function base64ToByteArray(src) {
var decode = atob(src); var decode = atob(src);
const byteNumbers = new Array(decode.length); const byteNumbers = new Array(decode.length);
@ -208,6 +208,7 @@ function playground_text(playground) {
} }
return new Uint8Array(byteNumbers); return new Uint8Array(byteNumbers);
} }
async function processHTML([htmlSrc, jsSrc, { wasm_js, wasm_bg }]) { async function processHTML([htmlSrc, jsSrc, { wasm_js, wasm_bg }]) {
var src = rewriteJS(jsSrc, wasm_js, wasm_bg); var src = rewriteJS(jsSrc, wasm_js, wasm_bg);
var jsBlob = createObjectURL(src, "application/javascript"); var jsBlob = createObjectURL(src, "application/javascript");
@ -235,6 +236,17 @@ function playground_text(playground) {
return URL.createObjectURL(new Blob([src], { type: mime })); return URL.createObjectURL(new Blob([src], { type: mime }));
} }
function createIFrame(src) {
var iframe = document.createElement('iframe');
iframe.style.height = "100%";
iframe.style.width = "100%";
iframe.style.padding = 0;
iframe.style.margin = 0;
iframe.style.border = 0;
iframe.src = createObjectURL(src, "text/html");
return iframe
}
// Syntax highlighting Configuration // Syntax highlighting Configuration
hljs.configure({ hljs.configure({
tabReplace: ' ', // 4 spaces tabReplace: ' ', // 4 spaces