Skip to content

Commit

Permalink
Merge pull request #221 from ilgonmic/ilgonmic/kconf-platform-release
Browse files Browse the repository at this point in the history
Ilgonmic/kconf platform release
  • Loading branch information
zoobestik authored Oct 24, 2024
2 parents 8f1f27c + 971aa5a commit 8b5a333
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
33 changes: 5 additions & 28 deletions src/executable-code/executable-fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import directives from 'monkberry-directives';
import 'monkberry-events';
import ExecutableCodeTemplate from './executable-fragment.monk';
import WebDemoApi from '../webdemo-api';
import { executeJs } from '../js-executor/execute-es-module';

import {
isJavaRelated,
Expand Down Expand Up @@ -314,13 +313,14 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
}

onConsoleCloseButtonEnter() {
const { jsLibs, onCloseConsole, targetPlatform } = this.state;
const { jsLibs, onCloseConsole, targetPlatform, compilerVersion } = this.state;
// creates a new iframe and removes the old one, thereby stops execution of any running script
if (isJsRelated(targetPlatform) || isWasmRelated(targetPlatform))
this.jsExecutor.reloadIframeScripts(
jsLibs,
this.getNodeForMountIframe(),
targetPlatform,
compilerVersion,
);
this.update({ output: '', openConsole: false, exception: null });
if (onCloseConsole) onCloseConsole();
Expand Down Expand Up @@ -409,35 +409,12 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
jsLibs,
this.getNodeForMountIframe(),
targetPlatform,
compilerVersion,
);
const additionalRequests = [];
if (targetPlatform === TargetPlatforms.COMPOSE_WASM) {
if (!this.jsExecutor.skikoImports) {
const skikoImport = fetch(API_URLS.SKIKO_MJS(compilerVersion), {
method: 'GET',
headers: {
'Content-Type': 'text/javascript',
},
})
.then((script) => script.text())
.then((script) =>
script.replace(
'new URL("skiko.wasm",import.meta.url).href',
`'${API_URLS.SKIKO_WASM(compilerVersion)}'`,
),
)
.then(async (skikoCode) => {
return await executeJs(
this.jsExecutor.iframe.contentWindow,
skikoCode,
);
})
.then((skikoImports) => {
this.jsExecutor.skikoImports = skikoImports;
this.jsExecutor.iframe.contentWindow.skikoImports = skikoImports;
});

additionalRequests.push(skikoImport);
if (!this.jsExecutor.skikoImport) {
additionalRequests.push(this.jsExecutor.skikoImport);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/js-executor/execute-es-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function executeWasmCode(container, jsCode, wasmCode) {
export async function executeWasmCodeWithSkiko(container, jsCode, wasmCode) {
const newCode = prepareJsCode(jsCode)
.replaceAll(
"imports['./skiko.mjs'] ?? await import('./skiko.mjs')",
"imports['./skiko.mjs']",
"window.skikoImports"
);
return execute(container, newCode, wasmCode);
Expand Down Expand Up @@ -35,6 +35,10 @@ function prepareJsCode(jsCode) {
"instantiateStreaming(fetch(wasmFilePath), importObject)).instance;",
"instantiate(window.wasmCode, importObject)).instance;\nwindow.wasmCode = undefined;"
)
.replace(
"instantiateStreaming(fetch(new URL('./playground.wasm',import.meta.url).href), importObject)).instance;",
"instantiate(window.wasmCode, importObject)).instance;\nwindow.wasmCode = undefined;"
)
.replace(
"const importObject = {",
"js_code['kotlin.io.printImpl'] = (message) => bufferedOutput.buffer += message\n" +
Expand Down
42 changes: 34 additions & 8 deletions src/js-executor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { API_URLS } from '../config';
import { showJsException } from '../view/output-view';
import { processingHtmlBrackets } from '../utils';
import { isWasmRelated, TargetPlatforms } from '../utils/platforms';
import { executeWasmCode, executeWasmCodeWithSkiko } from './execute-es-module';
import { executeJs, executeWasmCode, executeWasmCodeWithSkiko } from './execute-es-module';
import { fetch } from "whatwg-fetch";

const INIT_SCRIPT =
'if(kotlin.BufferedOutput!==undefined){kotlin.out = new kotlin.BufferedOutput()}' +
Expand All @@ -25,7 +26,7 @@ const normalizeJsVersion = (version) => {
export default class JsExecutor {
constructor(kotlinVersion) {
this.kotlinVersion = kotlinVersion;
this.skikoImports = undefined;
this.skikoImport = undefined;
}

async executeJsCode(
Expand Down Expand Up @@ -63,6 +64,10 @@ export default class JsExecutor {
return onError && onError.apply(this, arguments);
};

// It is necessary to work in Firefox
// for some reason resize function in Compose does not work in Firefox in invisible block
this.iframe.style.display = 'block';

const result = await this.executeWasm(
jsCode,
wasm,
Expand All @@ -71,7 +76,9 @@ export default class JsExecutor {
processError,
);

if (!exception) {
if (exception) {
this.iframe.style.display = 'none';
} else {
this.iframe.style.display = 'block';
if (outputHeight) this.iframe.style.height = `${outputHeight}px`;
}
Expand Down Expand Up @@ -138,7 +145,7 @@ export default class JsExecutor {
return new Promise((resolve) => setTimeout(resolve, ms));
}

reloadIframeScripts(jsLibs, node, targetPlatform) {
reloadIframeScripts(jsLibs, node, targetPlatform, compilerVersion) {
if (this.iframe !== undefined) {
node.removeChild(this.iframe);
}
Expand Down Expand Up @@ -170,10 +177,29 @@ export default class JsExecutor {
}
}
if (targetPlatform === TargetPlatforms.COMPOSE_WASM) {
if (this.skikoImports) {
this.iframe.contentWindow.skikoImports = this.skikoImports;
}
this.iframe.height = '1000';
this.skikoImport = fetch(API_URLS.SKIKO_MJS(compilerVersion), {
method: 'GET',
headers: {
'Content-Type': 'text/javascript',
}
})
.then(script => script.text())
.then(script => script.replace(
"new URL(\"skiko.wasm\",import.meta.url).href",
`'${API_URLS.SKIKO_WASM(compilerVersion)}'`
))
.then(async skikoCode => {
return await executeJs(
this.iframe.contentWindow,
skikoCode,
);
}
)
.then(skikoImports => {
this.iframe.contentWindow.skikoImports = skikoImports;
});

this.iframe.height = "1000"
iframeDoc.write(`<canvas height="1000" id="ComposeTarget"></canvas>`);
}
iframeDoc.write('<body style="margin: 0; overflow: hidden;"></body>');
Expand Down
Loading

0 comments on commit 8b5a333

Please sign in to comment.