@@ -124,6 +124,9 @@ function playground_text(playground) {
124
124
125
125
result_block . innerText = "Running..." ;
126
126
127
+ params = {
128
+ code : text
129
+ }
127
130
// fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
128
131
// headers: {
129
132
// 'Content-Type': "application/json",
@@ -132,35 +135,79 @@ function playground_text(playground) {
132
135
// mode: 'cors',
133
136
// body: JSON.stringify(params)
134
137
// })
135
- new Promise ( ( resolve , reject ) => {
136
- setTimeout ( ( ) => {
137
- resolve ( "foo" ) ;
138
- } , 200 )
138
+ prepareSandbox ( params ) . then ( src => processHTML ( src ) ) . then ( html => {
139
+ 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%" ;
144
+ iframe . style . height = "100%" ;
145
+ iframe . border = 0 ;
146
+ iframe . scrolling = "no" ;
147
+ doc . open ( ) . write ( html ) ;
148
+ doc . close ( ) ;
139
149
} )
140
- // .then(response => response.json())
141
- // .then(response => result_block.innerText = response.result)
142
- // .then(response => result_block.innerHTML = "<div style=\"height: 300px;background-color: red;\"></div>")
143
- // .then(response => result_block.innerHTML = " <div id=\"button_click\"></div><script type=\"module\" src=\"wasm-test.js\"></script>")
144
- . then ( response => {
145
- result_block . innerText = "" ;
146
- var iframe = result_block . appendChild ( document . createElement ( 'iframe' ) ) ,
147
- doc = iframe . contentWindow . document ;
148
- iframe . id = "wasm-rendering" ;
149
- iframe . style . width = "100%" ;
150
- iframe . style . height = "100%" ;
151
- var xhr = new XMLHttpRequest ( ) ;
152
- xhr . open ( 'GET' , 'iframe.html' , true ) ;
153
- xhr . onreadystatechange = function ( ) {
154
- if ( this . readyState !== 4 ) return ;
155
- if ( this . status !== 200 ) return ; // or whatever error handling you want
156
- var html = this . responseText ;
157
- doc . open ( ) . write ( html ) ;
158
- doc . close ( ) ;
159
- } ;
160
- xhr . send ( ) ;
150
+ }
151
+ async function prepareSandbox ( params ) {
152
+ var wasmResult = fetch_with_timeout ( "http://192.168.217.100:9999/wasm-pack" , {
153
+ headers : {
154
+ 'Content-Type' : "application/json" ,
155
+ } ,
156
+ method : 'POST' ,
157
+ mode : 'cors' ,
158
+ body : JSON . stringify ( params )
159
+ } )
160
+ . then ( response => response . json ( ) )
161
+ . then ( ( { wasm_js, wasm_bg } ) => {
162
+ var wasm_bg_blob = base64ToByteArray ( wasm_bg ) ;
163
+ return {
164
+ wasm_js : atob ( wasm_js ) ,
165
+ wasm_bg : wasm_bg_blob
166
+ }
161
167
} )
162
168
. catch ( error => result_block . innerText = "Playground Communication: " + error . message ) ;
163
169
170
+ var htmlSrc = fetch ( new Request ( "iframe.html" ) )
171
+ . then ( response => response . text ( ) ) ;
172
+ var jsSrc = fetch ( new Request ( "wasm-entry.mjs" ) )
173
+ . then ( response => response . text ( ) ) ;
174
+
175
+ return Promise . all ( [ htmlSrc , jsSrc , wasmResult ] )
176
+ . catch ( error => console . log ( error ) ) ;
177
+ }
178
+ function base64ToByteArray ( src ) {
179
+ var decode = atob ( src ) ;
180
+ const byteNumbers = new Array ( decode . length ) ;
181
+ for ( let i = 0 ; i < decode . length ; i ++ ) {
182
+ byteNumbers [ i ] = decode . charCodeAt ( i ) ;
183
+ }
184
+ return new Uint8Array ( byteNumbers ) ;
185
+ }
186
+ async function processHTML ( [ htmlSrc , jsSrc , { wasm_js, wasm_bg } ] ) {
187
+ var src = rewriteJS ( jsSrc , wasm_js , wasm_bg ) ;
188
+ var blob = new Blob ( [ src ] , { type : "application/javascript" } ) ;
189
+ var jsBlob = URL . createObjectURL ( blob ) ;
190
+ return htmlSrc . replace ( / \b s r c \s * = \s * [ ' " ] ( .+ ?) [ ' " ] / g, ( all , path ) => {
191
+ return `src="${ jsBlob } "` ;
192
+ } ) ;
193
+ }
194
+
195
+ 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 ) ;
201
+
202
+ // replace wasm.js
203
+ src = src . replace ( / \b f r o m \s + [ ' " ] ( .+ ?) [ ' " ] ( \s * [ ; \n ] ) / g, ( all , path , sep ) => {
204
+ return `from "${ wasmJSBlob } "${ sep } ` ;
205
+ } )
206
+ // replace `input` of init to object URL
207
+ src = src . replace ( / \( [ ' " ] ( .+ ?) [ ' " ] \) / g, ( all , url ) => {
208
+ return `("${ bgWasmBlob } ")` ;
209
+ } )
210
+ return src
164
211
}
165
212
166
213
// Syntax highlighting Configuration
0 commit comments