11import './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' ;
813
914const INIT_SCRIPT =
1015 'if(kotlin.BufferedOutput!==undefined){kotlin.out = new kotlin.BufferedOutput()}' +
@@ -38,6 +43,7 @@ export default class JsExecutor {
3843 theme ,
3944 onError ,
4045 additionalRequestsResults ,
46+ compilerVersion ,
4147 ) {
4248 if ( platform === TargetPlatforms . SWIFT_EXPORT ) {
4349 return `<span class="standard-output ${ theme } "><div class="result-code">${ jsCode } </span>` ;
@@ -90,18 +96,22 @@ export default class JsExecutor {
9096 return result ;
9197 }
9298 }
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+ ) ;
94107 }
95108
96- async execute ( jsCode , jsLibs , theme , onError , platform ) {
109+ async execute ( jsCode , jsLibs , theme , onError , platform , compilerVersion ) {
97110 const loadedScripts = (
98111 this . iframe . contentDocument || this . iframe . document
99112 ) . 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 ) ) {
105115 // 2 scripts by default: INIT_SCRIPT + kotlin stdlib
106116 offset = 2 ;
107117 }
@@ -110,8 +120,8 @@ export default class JsExecutor {
110120 const output = this . iframe . contentWindow . eval ( jsCode ) ;
111121 return output
112122 ? `<span class="standard-output ${ theme } ">${ processingHtmlBrackets (
113- output ,
114- ) } </span>`
123+ output ,
124+ ) } </span>`
115125 : '' ;
116126 } catch ( e ) {
117127 if ( onError ) onError ( ) ;
@@ -120,24 +130,39 @@ export default class JsExecutor {
120130 }
121131 }
122132 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+ ) ;
124141 }
125142
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+ ) {
127152 try {
128153 const exports = await executor (
129154 this . iframe . contentWindow ,
130155 jsCode ,
131156 wasmCode ,
132157 ) ;
133- await exports . instantiate ( { " playground.master" : imports } ) ;
158+ await exports . instantiate ( { ' playground.master' : imports } ) ;
134159 const bufferedOutput = output ?? exports . bufferedOutput ;
135160 const outputString = bufferedOutput . buffer ;
136161 bufferedOutput . buffer = '' ;
137162 return outputString
138163 ? `<span class="standard-output ${ theme } ">${ processingHtmlBrackets (
139- outputString ,
140- ) } </span>`
164+ outputString ,
165+ ) } </span>`
141166 : '' ;
142167 } catch ( e ) {
143168 if ( onError ) onError ( ) ;
@@ -159,10 +184,7 @@ export default class JsExecutor {
159184 node . appendChild ( this . iframe ) ;
160185 let iframeDoc = this . iframe . contentDocument || this . iframe . document ;
161186 iframeDoc . open ( ) ;
162- if (
163- targetPlatform === TargetPlatforms . JS ||
164- targetPlatform === TargetPlatforms . CANVAS
165- ) {
187+ if ( isJsLegacy ( targetPlatform , compilerVersion ) ) {
166188 const kotlinScript =
167189 API_URLS . KOTLIN_JS +
168190 `${ normalizeJsVersion ( this . kotlinVersion ) } /kotlin.js` ;
@@ -175,84 +197,88 @@ export default class JsExecutor {
175197 for ( let lib of jsLibs ) {
176198 iframeDoc . write ( "<script src='" + lib + "'></script>" ) ;
177199 }
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+ ) ;
183203 }
184204 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' ] ;
191211
192212 const skikoExports = fetch ( API_URLS . SKIKO_MJS ( skikoVersion ) , {
193213 method : 'GET' ,
194214 headers : {
195215 '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 ) ) ;
208229
209- const stdlibVersion = versions [ " stdlib" ] ;
230+ const stdlibVersion = versions [ ' stdlib' ] ;
210231
211232 const stdlibExports = fetch ( API_URLS . STDLIB_MJS ( stdlibVersion ) , {
212233 method : 'GET' ,
213234 headers : {
214235 'Content-Type' : 'text/javascript' ,
215- }
216- } ) . then ( script => script . text ( ) )
217- . then ( script =>
236+ } ,
237+ } )
238+ . then ( ( script ) => script . text ( ) )
239+ . then ( ( script ) =>
218240 // necessary to load stdlib.wasm before its initialization to parallelize
219241 // 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
231245 )
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+ ) ,
232254 )
255+ . then ( ( stdlibCode ) =>
256+ executeWasmCodeWithSkiko ( this . iframe . contentWindow , stdlibCode ) ,
257+ )
258+ . then ( ( stdlibCode ) =>
259+ executeWasmCodeWithSkiko ( this . iframe . contentWindow , stdlibCode ) ,
260+ ) ;
233261
234- return Promise . all ( [ skikoExports , stdlibExports ] )
235- } )
262+ return Promise . all ( [ skikoExports , stdlibExports ] ) ;
263+ } ) ;
236264
237265 this . stdlibExports = skikoStdlib
238266 . 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+ } )
247274 . 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+ } ) ;
254280
255- this . iframe . height = " 1000"
281+ this . iframe . height = ' 1000' ;
256282 iframeDoc . write ( `<canvas height="1000" id="ComposeTarget"></canvas>` ) ;
257283 }
258284 iframeDoc . write ( '<body style="margin: 0; overflow: hidden;"></body>' ) ;
@@ -264,25 +290,39 @@ function fixedSkikoExports(skikoExports) {
264290 return {
265291 ...skikoExports ,
266292 org_jetbrains_skia_Bitmap__1nGetPixmap : function ( ) {
267- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
293+ console . log (
294+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
295+ ) ;
268296 } ,
269297 org_jetbrains_skia_Bitmap__1nIsVolatile : function ( ) {
270- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
298+ console . log (
299+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
300+ ) ;
271301 } ,
272302 org_jetbrains_skia_Bitmap__1nSetVolatile : function ( ) {
273- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
303+ console . log (
304+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
305+ ) ;
274306 } ,
275307 org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer : function ( ) {
276- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
308+ console . log (
309+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
310+ ) ;
277311 } ,
278312 org_jetbrains_skia_TextBlobBuilderRunHandler__1nMake : function ( ) {
279- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
313+ console . log (
314+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
315+ ) ;
280316 } ,
281317 org_jetbrains_skia_TextBlobBuilderRunHandler__1nMakeBlob : function ( ) {
282- console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
318+ console . log (
319+ 'org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer' ,
320+ ) ;
283321 } ,
284322 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+ } ;
288328}
0 commit comments