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 } 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 { isWasmRelated , TargetPlatforms } from '../utils/platforms' ;
6+ import { executeJs , executeWasmCode , executeWasmCodeWithSkiko , executeWasmCodeWithStdlib } from './execute-es-module' ;
7+ import { fetch } from "whatwg-fetch" ;
88
99const INIT_SCRIPT =
1010 'if(kotlin.BufferedOutput!==undefined){kotlin.out = new kotlin.BufferedOutput()}' +
@@ -26,7 +26,7 @@ const normalizeJsVersion = (version) => {
2626export default class JsExecutor {
2727 constructor ( kotlinVersion ) {
2828 this . kotlinVersion = kotlinVersion ;
29- this . skikoImport = undefined ;
29+ this . stdlibExports = undefined ;
3030 }
3131
3232 async executeJsCode (
@@ -37,6 +37,7 @@ export default class JsExecutor {
3737 outputHeight ,
3838 theme ,
3939 onError ,
40+ additionalRequestsResults ,
4041 ) {
4142 if ( platform === TargetPlatforms . SWIFT_EXPORT ) {
4243 return `<span class="standard-output ${ theme } "><div class="result-code">${ jsCode } </span>` ;
@@ -68,12 +69,15 @@ export default class JsExecutor {
6869 // for some reason resize function in Compose does not work in Firefox in invisible block
6970 this . iframe . style . display = 'block' ;
7071
72+ const additionalRequestsResult = additionalRequestsResults [ 0 ] ;
7173 const result = await this . executeWasm (
7274 jsCode ,
7375 wasm ,
74- executeWasmCodeWithSkiko ,
76+ executeWasmCodeWithStdlib ,
7577 theme ,
7678 processError ,
79+ additionalRequestsResult . stdlib ,
80+ additionalRequestsResult . output ,
7781 ) ;
7882
7983 if ( exception ) {
@@ -106,8 +110,8 @@ export default class JsExecutor {
106110 const output = this . iframe . contentWindow . eval ( jsCode ) ;
107111 return output
108112 ? `<span class="standard-output ${ theme } ">${ processingHtmlBrackets (
109- output ,
110- ) } </span>`
113+ output ,
114+ ) } </span>`
111115 : '' ;
112116 } catch ( e ) {
113117 if ( onError ) onError ( ) ;
@@ -119,20 +123,21 @@ export default class JsExecutor {
119123 return await this . execute ( jsCode , jsLibs , theme , onError , platform ) ;
120124 }
121125
122- async executeWasm ( jsCode , wasmCode , executor , theme , onError ) {
126+ async executeWasm ( jsCode , wasmCode , executor , theme , onError , imports , output ) {
123127 try {
124128 const exports = await executor (
125129 this . iframe . contentWindow ,
126130 jsCode ,
127131 wasmCode ,
128132 ) ;
129- await exports . instantiate ( ) ;
130- const output = exports . bufferedOutput . buffer ;
131- exports . bufferedOutput . buffer = '' ;
132- return output
133+ await exports . instantiate ( imports ) ;
134+ const bufferedOutput = output ?? exports . bufferedOutput ;
135+ const outputString = bufferedOutput . buffer ;
136+ bufferedOutput . buffer = '' ;
137+ return outputString
133138 ? `<span class="standard-output ${ theme } ">${ processingHtmlBrackets (
134- output ,
135- ) } </span>`
139+ outputString ,
140+ ) } </span>`
136141 : '' ;
137142 } catch ( e ) {
138143 if ( onError ) onError ( ) ;
@@ -177,27 +182,58 @@ export default class JsExecutor {
177182 }
178183 }
179184 if ( targetPlatform === TargetPlatforms . COMPOSE_WASM ) {
180- this . skikoImport = fetch ( API_URLS . SKIKO_MJS ( compilerVersion ) , {
185+
186+ const skikoExports = fetch ( API_URLS . SKIKO_MJS ( compilerVersion ) , {
181187 method : 'GET' ,
182188 headers : {
183189 'Content-Type' : 'text/javascript' ,
184190 }
185- } )
186- . then ( script => script . text ( ) )
191+ } ) . then ( script => script . text ( ) )
187192 . then ( script => script . replace (
188193 "new URL(\"skiko.wasm\",import.meta.url).href" ,
189194 `'${ API_URLS . SKIKO_WASM ( compilerVersion ) } '`
190195 ) )
191- . then ( async skikoCode => {
192- return await executeJs (
193- this . iframe . contentWindow ,
194- skikoCode ,
195- ) ;
196+ . then ( async skikoCode =>
197+ executeJs (
198+ this . iframe . contentWindow ,
199+ skikoCode ,
200+ ) )
201+ . then ( skikoExports => fixedSkikoExports ( skikoExports ) )
202+
203+ const stdlibExports = fetch ( API_URLS . STDLIB_MJS ( compilerVersion ) , {
204+ method : 'GET' ,
205+ headers : {
206+ 'Content-Type' : 'text/javascript' ,
207+ }
208+ } ) . then ( script => script . text ( ) )
209+ . then ( script => script . replace (
210+ "new URL('./stdlib.wasm',import.meta.url).href" ,
211+ `'${ API_URLS . STDLIB_WASM ( compilerVersion ) } '`
212+ ) )
213+ . then ( stdlibCode =>
214+ executeWasmCodeWithSkiko (
215+ this . iframe . contentWindow ,
216+ stdlibCode ,
217+ )
218+ )
219+
220+ this . stdlibExports = Promise . all ( [ skikoExports , stdlibExports ] )
221+ . then ( async ( [ skikoExportsResult , stdlibExportsResult ] ) => {
222+ return [
223+ await stdlibExportsResult . stdlib ( {
224+ "./skiko.mjs" : skikoExportsResult
225+ } ) ,
226+ stdlibExportsResult
227+ ]
228+ }
229+ )
230+ . then ( ( [ stdlibResult , outputResult ] ) => {
231+ return {
232+ "stdlib" : stdlibResult . exports ,
233+ "output" : outputResult . bufferedOutput
234+ }
196235 }
197236 )
198- . then ( skikoImports => {
199- this . iframe . contentWindow . skikoImports = skikoImports ;
200- } ) ;
201237
202238 this . iframe . height = "1000"
203239 iframeDoc . write ( `<canvas height="1000" id="ComposeTarget"></canvas>` ) ;
@@ -206,3 +242,30 @@ export default class JsExecutor {
206242 iframeDoc . close ( ) ;
207243 }
208244}
245+
246+ function fixedSkikoExports ( skikoExports ) {
247+ return {
248+ ...skikoExports ,
249+ org_jetbrains_skia_Bitmap__1nGetPixmap : function ( ) {
250+ console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
251+ } ,
252+ org_jetbrains_skia_Bitmap__1nIsVolatile : function ( ) {
253+ console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
254+ } ,
255+ org_jetbrains_skia_Bitmap__1nSetVolatile : function ( ) {
256+ console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
257+ } ,
258+ org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer : function ( ) {
259+ console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
260+ } ,
261+ org_jetbrains_skia_TextBlobBuilderRunHandler__1nMake : function ( ) {
262+ console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
263+ } ,
264+ org_jetbrains_skia_TextBlobBuilderRunHandler__1nMakeBlob : function ( ) {
265+ console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
266+ } ,
267+ org_jetbrains_skia_svg_SVGCanvasKt__1nMake : function ( ) {
268+ console . log ( "org_jetbrains_skia_TextBlobBuilderRunHandler__1nGetFinalizer" )
269+ }
270+ }
271+ }
0 commit comments