Skip to content

Commit b206ed0

Browse files
committed
Add wasm class to determine type of request
1 parent 330085b commit b206ed0

File tree

1 file changed

+60
-31
lines changed

1 file changed

+60
-31
lines changed

src/theme/book.js

+60-31
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ function playground_text(playground) {
5959
win: "Ctrl-Enter",
6060
mac: "Ctrl-Enter"
6161
},
62-
exec: _editor => run_rust_code(playground_block)
62+
exec: _editor => {
63+
console.log(code_block.classList.contains("wasm"));
64+
if (code_block.classList.contains("wasm")) {
65+
run_wasm_pack_code(playground_block);
66+
} else {
67+
run_rust_code(playground_block);
68+
}
69+
}
6370
});
6471
}
6572
}
@@ -124,32 +131,51 @@ function playground_text(playground) {
124131

125132
result_block.innerText = "Running...";
126133

127-
params = {
128-
code: text
134+
fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
135+
headers: {
136+
'Content-Type': "application/json",
137+
},
138+
method: 'POST',
139+
mode: 'cors',
140+
body: JSON.stringify(params)
141+
})
142+
.then(response => response.json())
143+
.then(response => result_block.innerText = response.result)
144+
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
145+
}
146+
147+
function run_wasm_pack_code(code_block) {
148+
var result_block = code_block.querySelector(".result");
149+
if (!result_block) {
150+
result_block = document.createElement('code');
151+
result_block.className = 'result hljs language-bash';
152+
153+
code_block.append(result_block);
129154
}
130-
// fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
131-
// headers: {
132-
// 'Content-Type': "application/json",
133-
// },
134-
// method: 'POST',
135-
// mode: 'cors',
136-
// body: JSON.stringify(params)
137-
// })
155+
156+
let text = playground_text(code_block);
157+
158+
var params = {
159+
code: text,
160+
};
161+
162+
result_block.innerText = "Running...";
163+
138164
prepareSandbox(params).then(src => processHTML(src)).then(html => {
139165
result_block.innerText = "";
140-
var iframe = result_block.appendChild(document.createElement('iframe')),
141-
doc = iframe.contentWindow.document;
142-
iframe.id = "wasm-rendering";
143-
iframe.style.width = "100%";
166+
var iframe = document.createElement('iframe');
144167
iframe.style.height = "100%";
145-
iframe.border = 0;
146-
iframe.scrolling = "no";
147-
doc.open().write(html);
148-
doc.close();
149-
})
168+
iframe.style.width = "100%";
169+
iframe.style.padding = 0;
170+
iframe.style.margin = 0;
171+
iframe.style.border = 0;
172+
iframe.src = createObjectURL(html, "text/html");
173+
result_block.appendChild(iframe);
174+
});
150175
}
176+
// Greatly inspired from WebAssemblyStudio
151177
async function prepareSandbox(params) {
152-
var wasmResult = fetch_with_timeout("http://192.168.217.100:9999/wasm-pack", {
178+
var wasmResult = fetch_with_timeout("http://127.0.0.1:9999/wasm-pack", {
153179
headers: {
154180
'Content-Type': "application/json",
155181
},
@@ -159,10 +185,9 @@ function playground_text(playground) {
159185
})
160186
.then(response => response.json())
161187
.then(({ wasm_js, wasm_bg }) => {
162-
var wasm_bg_blob = base64ToByteArray(wasm_bg);
163188
return {
164189
wasm_js: atob(wasm_js),
165-
wasm_bg: wasm_bg_blob
190+
wasm_bg: base64ToByteArray(wasm_bg)
166191
}
167192
})
168193
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
@@ -185,19 +210,15 @@ function playground_text(playground) {
185210
}
186211
async function processHTML([htmlSrc, jsSrc, { wasm_js, wasm_bg }]) {
187212
var src = rewriteJS(jsSrc, wasm_js, wasm_bg);
188-
var blob = new Blob([src], { type: "application/javascript" });
189-
var jsBlob = URL.createObjectURL(blob);
213+
var jsBlob = createObjectURL(src, "application/javascript");
190214
return htmlSrc.replace(/\bsrc\s*=\s*['"](.+?)['"]/g, (all, path) => {
191215
return `src="${jsBlob}"`;
192216
});
193217
}
194218

195219
function rewriteJS(src, wasmJS, bgWasm) {
196-
var blob = new Blob([wasmJS], { type: "application/javascript" });
197-
var wasmJSBlob = URL.createObjectURL(blob);
198-
199-
var blob = new Blob([bgWasm], { type: "application/wasm" });
200-
var bgWasmBlob = URL.createObjectURL(blob);
220+
var wasmJSBlob = createObjectURL(wasmJS, "application/javascript");
221+
var bgWasmBlob = createObjectURL(bgWasm, "application/wasm");
201222

202223
// replace wasm.js
203224
src = src.replace(/\bfrom\s+['"](.+?)['"](\s*[;\n])/g, (all, path, sep) => {
@@ -209,6 +230,10 @@ function playground_text(playground) {
209230
})
210231
return src
211232
}
233+
234+
function createObjectURL(src, mime) {
235+
return URL.createObjectURL(new Blob([src], { type: mime }));
236+
}
212237

213238
// Syntax highlighting Configuration
214239
hljs.configure({
@@ -313,7 +338,11 @@ function playground_text(playground) {
313338

314339
buttons.insertBefore(runCodeButton, buttons.firstChild);
315340
runCodeButton.addEventListener('click', function (e) {
316-
run_rust_code(pre_block);
341+
if (code_block.classList.contains("wasm")) {
342+
run_wasm_pack_code(pre_block);
343+
} else {
344+
run_rust_code(pre_block);
345+
}
317346
});
318347

319348
if (window.playground_copyable) {

0 commit comments

Comments
 (0)