@@ -59,7 +59,14 @@ function playground_text(playground) {
59
59
win : "Ctrl-Enter" ,
60
60
mac : "Ctrl-Enter"
61
61
} ,
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
+ }
63
70
} ) ;
64
71
}
65
72
}
@@ -124,32 +131,51 @@ function playground_text(playground) {
124
131
125
132
result_block . innerText = "Running..." ;
126
133
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 ) ;
129
154
}
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
+
138
164
prepareSandbox ( params ) . then ( src => processHTML ( src ) ) . then ( html => {
139
165
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' ) ;
144
167
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
+ } ) ;
150
175
}
176
+ // Greatly inspired from WebAssemblyStudio
151
177
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" , {
153
179
headers : {
154
180
'Content-Type' : "application/json" ,
155
181
} ,
@@ -159,10 +185,9 @@ function playground_text(playground) {
159
185
} )
160
186
. then ( response => response . json ( ) )
161
187
. then ( ( { wasm_js, wasm_bg } ) => {
162
- var wasm_bg_blob = base64ToByteArray ( wasm_bg ) ;
163
188
return {
164
189
wasm_js : atob ( wasm_js ) ,
165
- wasm_bg : wasm_bg_blob
190
+ wasm_bg : base64ToByteArray ( wasm_bg )
166
191
}
167
192
} )
168
193
. catch ( error => result_block . innerText = "Playground Communication: " + error . message ) ;
@@ -185,19 +210,15 @@ function playground_text(playground) {
185
210
}
186
211
async function processHTML ( [ htmlSrc , jsSrc , { wasm_js, wasm_bg } ] ) {
187
212
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" ) ;
190
214
return htmlSrc . replace ( / \b s r c \s * = \s * [ ' " ] ( .+ ?) [ ' " ] / g, ( all , path ) => {
191
215
return `src="${ jsBlob } "` ;
192
216
} ) ;
193
217
}
194
218
195
219
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" ) ;
201
222
202
223
// replace wasm.js
203
224
src = src . replace ( / \b f r o m \s + [ ' " ] ( .+ ?) [ ' " ] ( \s * [ ; \n ] ) / g, ( all , path , sep ) => {
@@ -209,6 +230,10 @@ function playground_text(playground) {
209
230
} )
210
231
return src
211
232
}
233
+
234
+ function createObjectURL ( src , mime ) {
235
+ return URL . createObjectURL ( new Blob ( [ src ] , { type : mime } ) ) ;
236
+ }
212
237
213
238
// Syntax highlighting Configuration
214
239
hljs . configure ( {
@@ -313,7 +338,11 @@ function playground_text(playground) {
313
338
314
339
buttons . insertBefore ( runCodeButton , buttons . firstChild ) ;
315
340
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
+ }
317
346
} ) ;
318
347
319
348
if ( window . playground_copyable ) {
0 commit comments