1
1
import './index.scss' ;
2
- import { API_URLS } from '../config' ;
3
- import { showJsException } from '../view/output-view' ;
4
- import { processingHtmlBrackets } from '../utils' ;
5
- import { isWasmRelated , TargetPlatforms } from '../utils/platforms' ;
6
- import { executeJs , executeWasmCode , executeWasmCodeWithSkiko , executeWasmCodeWithStdlib } from './execute-es-module' ;
7
- import { fetch } from "whatwg-fetch" ;
2
+ import { API_URLS } from '../config' ;
3
+ import { showJsException } from '../view/output-view' ;
4
+ import { processingHtmlBrackets } from '../utils' ;
5
+ import { isJsLegacy , isWasmRelated , TargetPlatforms } from '../utils/platforms' ;
6
+ import {
7
+ executeJs ,
8
+ executeWasmCode ,
9
+ executeWasmCodeWithSkiko ,
10
+ executeWasmCodeWithStdlib ,
11
+ } from './execute-es-module' ;
12
+ import { fetch } from 'whatwg-fetch' ;
8
13
9
14
const INIT_SCRIPT =
10
15
'if(kotlin.BufferedOutput!==undefined){kotlin.out = new kotlin.BufferedOutput()}' +
@@ -38,6 +43,7 @@ export default class JsExecutor {
38
43
theme ,
39
44
onError ,
40
45
additionalRequestsResults ,
46
+ compilerVersion ,
41
47
) {
42
48
if ( platform === TargetPlatforms . SWIFT_EXPORT ) {
43
49
return `<span class="standard-output ${ theme } "><div class="result-code">${ jsCode } </span>` ;
@@ -90,18 +96,22 @@ export default class JsExecutor {
90
96
return result ;
91
97
}
92
98
}
93
- return await this . execute ( jsCode , jsLibs , theme , onError , platform ) ;
99
+ return await this . execute (
100
+ jsCode ,
101
+ jsLibs ,
102
+ theme ,
103
+ onError ,
104
+ platform ,
105
+ compilerVersion ,
106
+ ) ;
94
107
}
95
108
96
- async execute ( jsCode , jsLibs , theme , onError , platform ) {
109
+ async execute ( jsCode , jsLibs , theme , onError , platform , compilerVersion ) {
97
110
const loadedScripts = (
98
111
this . iframe . contentDocument || this . iframe . document
99
112
) . getElementsByTagName ( 'script' ) . length ;
100
- let offset ;
101
- if ( platform === TargetPlatforms . JS_IR ) {
102
- // 1 scripts by default: INIT_SCRIPT_IR
103
- offset = 1 ;
104
- } else {
113
+ let offset = 1 ; // 1 scripts by default: INIT_SCRIPT_IR
114
+ if ( isJsLegacy ( platform , compilerVersion ) ) {
105
115
// 2 scripts by default: INIT_SCRIPT + kotlin stdlib
106
116
offset = 2 ;
107
117
}
@@ -110,8 +120,8 @@ export default class JsExecutor {
110
120
const output = this . iframe . contentWindow . eval ( jsCode ) ;
111
121
return output
112
122
? `<span class="standard-output ${ theme } ">${ processingHtmlBrackets (
113
- output ,
114
- ) } </span>`
123
+ output ,
124
+ ) } </span>`
115
125
: '' ;
116
126
} catch ( e ) {
117
127
if ( onError ) onError ( ) ;
@@ -120,24 +130,39 @@ export default class JsExecutor {
120
130
}
121
131
}
122
132
await this . timeout ( 400 ) ;
123
- return await this . execute ( jsCode , jsLibs , theme , onError , platform ) ;
133
+ return await this . execute (
134
+ jsCode ,
135
+ jsLibs ,
136
+ theme ,
137
+ onError ,
138
+ platform ,
139
+ compilerVersion ,
140
+ ) ;
124
141
}
125
142
126
- async executeWasm ( jsCode , wasmCode , executor , theme , onError , imports , output ) {
143
+ async executeWasm (
144
+ jsCode ,
145
+ wasmCode ,
146
+ executor ,
147
+ theme ,
148
+ onError ,
149
+ imports ,
150
+ output ,
151
+ ) {
127
152
try {
128
153
const exports = await executor (
129
154
this . iframe . contentWindow ,
130
155
jsCode ,
131
156
wasmCode ,
132
157
) ;
133
- await exports . instantiate ( { " playground.master" : imports } ) ;
158
+ await exports . instantiate ( { ' playground.master' : imports } ) ;
134
159
const bufferedOutput = output ?? exports . bufferedOutput ;
135
160
const outputString = bufferedOutput . buffer ;
136
161
bufferedOutput . buffer = '' ;
137
162
return outputString
138
163
? `<span class="standard-output ${ theme } ">${ processingHtmlBrackets (
139
- outputString ,
140
- ) } </span>`
164
+ outputString ,
165
+ ) } </span>`
141
166
: '' ;
142
167
} catch ( e ) {
143
168
if ( onError ) onError ( ) ;
@@ -159,10 +184,7 @@ export default class JsExecutor {
159
184
node . appendChild ( this . iframe ) ;
160
185
let iframeDoc = this . iframe . contentDocument || this . iframe . document ;
161
186
iframeDoc . open ( ) ;
162
- if (
163
- targetPlatform === TargetPlatforms . JS ||
164
- targetPlatform === TargetPlatforms . CANVAS
165
- ) {
187
+ if ( isJsLegacy ( targetPlatform , compilerVersion ) ) {
166
188
const kotlinScript =
167
189
API_URLS . KOTLIN_JS +
168
190
`${ normalizeJsVersion ( this . kotlinVersion ) } /kotlin.js` ;
@@ -175,84 +197,88 @@ export default class JsExecutor {
175
197
for ( let lib of jsLibs ) {
176
198
iframeDoc . write ( "<script src='" + lib + "'></script>" ) ;
177
199
}
178
- if ( targetPlatform === TargetPlatforms . JS_IR ) {
179
- iframeDoc . write ( `<script>${ INIT_SCRIPT_IR } </script>` ) ;
180
- } else {
181
- iframeDoc . write ( `<script>${ INIT_SCRIPT } </script>` ) ;
182
- }
200
+ iframeDoc . write (
201
+ `<script>${ isJsLegacy ( targetPlatform , compilerVersion ) ? INIT_SCRIPT : INIT_SCRIPT_IR } </script>` ,
202
+ ) ;
183
203
}
184
204
if ( targetPlatform === TargetPlatforms . COMPOSE_WASM ) {
185
-
186
- const skikoStdlib = fetch ( API_URLS . RESOURCE_VERSIONS ( ) , {
187
- method : 'GET'
188
- } ) . then ( response => response . json ( ) )
189
- . then ( versions => {
190
- const skikoVersion = versions [ " skiko" ] ;
205
+ const skikoStdlib = fetch ( API_URLS . RESOURCE_VERSIONS ( ) , {
206
+ method : 'GET' ,
207
+ } )
208
+ . then ( ( response ) => response . json ( ) )
209
+ . then ( ( versions ) => {
210
+ const skikoVersion = versions [ ' skiko' ] ;
191
211
192
212
const skikoExports = fetch ( API_URLS . SKIKO_MJS ( skikoVersion ) , {
193
213
method : 'GET' ,
194
214
headers : {
195
215
'Content-Type' : 'text/javascript' ,
196
- }
197
- } ) . then ( script => script . text ( ) )
198
- . then ( script => script . replace (
199
- "new URL(\"skiko.wasm\",import.meta.url).href" ,
200
- `'${ API_URLS . SKIKO_WASM ( skikoVersion ) } '`
201
- ) )
202
- . then ( skikoCode =>
203
- executeJs (
204
- this . iframe . contentWindow ,
205
- skikoCode ,
206
- ) )
207
- . then ( skikoExports => fixedSkikoExports ( skikoExports ) )
216
+ } ,
217
+ } )
218
+ . then ( ( script ) => script . text ( ) )
219
+ . then ( ( script ) =>
220
+ script . replace (
221
+ 'new URL("skiko.wasm",import.meta.url).href' ,
222
+ `'${ API_URLS . SKIKO_WASM ( skikoVersion ) } '` ,
223
+ ) ,
224
+ )
225
+ . then ( ( skikoCode ) =>
226
+ executeJs ( this . iframe . contentWindow , skikoCode ) ,
227
+ )
228
+ . then ( ( skikoExports ) => fixedSkikoExports ( skikoExports ) ) ;
208
229
209
- const stdlibVersion = versions [ " stdlib" ] ;
230
+ const stdlibVersion = versions [ ' stdlib' ] ;
210
231
211
232
const stdlibExports = fetch ( API_URLS . STDLIB_MJS ( stdlibVersion ) , {
212
233
method : 'GET' ,
213
234
headers : {
214
235
'Content-Type' : 'text/javascript' ,
215
- }
216
- } ) . then ( script => script . text ( ) )
217
- . then ( script =>
236
+ } ,
237
+ } )
238
+ . then ( ( script ) => script . text ( ) )
239
+ . then ( ( script ) =>
218
240
// necessary to load stdlib.wasm before its initialization to parallelize
219
241
// language=JavaScript
220
- ( `const stdlibWasm = fetch('${ API_URLS . STDLIB_WASM ( stdlibVersion ) } ');\n` + script ) . replace (
221
- "fetch(new URL('./stdlib_master.wasm',import.meta.url).href)" ,
222
- "stdlibWasm"
223
- ) . replace (
224
- "(extends) => { return { extends }; }" ,
225
- "(extends_) => { return { extends_ }; }"
226
- ) )
227
- . then ( stdlibCode =>
228
- executeWasmCodeWithSkiko (
229
- this . iframe . contentWindow ,
230
- stdlibCode ,
242
+ (
243
+ `const stdlibWasm = fetch('${ API_URLS . STDLIB_WASM ( stdlibVersion ) } ');\n` +
244
+ script
231
245
)
246
+ . replace (
247
+ "fetch(new URL('./stdlib_master.wasm',import.meta.url).href)" ,
248
+ 'stdlibWasm' ,
249
+ )
250
+ . replace (
251
+ '(extends) => { return { extends }; }' ,
252
+ '(extends_) => { return { extends_ }; }' ,
253
+ ) ,
232
254
)
255
+ . then ( ( stdlibCode ) =>
256
+ executeWasmCodeWithSkiko ( this . iframe . contentWindow , stdlibCode ) ,
257
+ )
258
+ . then ( ( stdlibCode ) =>
259
+ executeWasmCodeWithSkiko ( this . iframe . contentWindow , stdlibCode ) ,
260
+ ) ;
233
261
234
- return Promise . all ( [ skikoExports , stdlibExports ] )
235
- } )
262
+ return Promise . all ( [ skikoExports , stdlibExports ] ) ;
263
+ } ) ;
236
264
237
265
this . stdlibExports = skikoStdlib
238
266
. then ( async ( [ skikoExportsResult , stdlibExportsResult ] ) => {
239
- return [
240
- await stdlibExportsResult . instantiate ( {
241
- "./skiko.mjs" : skikoExportsResult
242
- } ) ,
243
- stdlibExportsResult
244
- ]
245
- }
246
- )
267
+ return [
268
+ await stdlibExportsResult . instantiate ( {
269
+ './skiko.mjs' : skikoExportsResult ,
270
+ } ) ,
271
+ stdlibExportsResult ,
272
+ ] ;
273
+ } )
247
274
. then ( ( [ stdlibResult , outputResult ] ) => {
248
- return {
249
- "stdlib" : stdlibResult . exports ,
250
- "output" : outputResult . bufferedOutput
251
- }
252
- }
253
- )
275
+ return {
276
+ stdlib : stdlibResult . exports ,
277
+ output : outputResult . bufferedOutput ,
278
+ } ;
279
+ } ) ;
254
280
255
- this . iframe . height = " 1000"
281
+ this . iframe . height = ' 1000' ;
256
282
iframeDoc . write ( `<canvas height="1000" id="ComposeTarget"></canvas>` ) ;
257
283
}
258
284
iframeDoc . write ( '<body style="margin: 0; overflow: hidden;"></body>' ) ;
@@ -264,25 +290,39 @@ function fixedSkikoExports(skikoExports) {
264
290
return {
265
291
...skikoExports ,
266
292
org_jetbrains_skia_Bitmap__1nGetPixmap : function ( ) {
267
- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
293
+ console . log (
294
+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
295
+ ) ;
268
296
} ,
269
297
org_jetbrains_skia_Bitmap__1nIsVolatile : function ( ) {
270
- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
298
+ console . log (
299
+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
300
+ ) ;
271
301
} ,
272
302
org_jetbrains_skia_Bitmap__1nSetVolatile : function ( ) {
273
- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
303
+ console . log (
304
+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
305
+ ) ;
274
306
} ,
275
307
org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer : function ( ) {
276
- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
308
+ console . log (
309
+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
310
+ ) ;
277
311
} ,
278
312
org_jetbrains_skia_TextBlobBuilderRunHandler__1nMake : function ( ) {
279
- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
313
+ console . log (
314
+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
315
+ ) ;
280
316
} ,
281
317
org_jetbrains_skia_TextBlobBuilderRunHandler__1nMakeBlob : function ( ) {
282
- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
318
+ console . log (
319
+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
320
+ ) ;
283
321
} ,
284
322
org_jetbrains_skia_svg_SVGCanvasKt__1nMake : function ( ) {
285
- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
286
- }
287
- }
323
+ console . log (
324
+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
325
+ ) ;
326
+ } ,
327
+ } ;
288
328
}
0 commit comments