in src/js-executor/index.js [153:260]
reloadIframeScripts(jsLibs, node, targetPlatform, compilerVersion) {
if (this.iframe !== undefined) {
node.removeChild(this.iframe);
}
this.iframe = document.createElement('iframe');
this.iframe.className = 'k2js-iframe';
node.appendChild(this.iframe);
let iframeDoc = this.iframe.contentDocument || this.iframe.document;
iframeDoc.open();
if (
targetPlatform === TargetPlatforms.JS ||
targetPlatform === TargetPlatforms.CANVAS
) {
const kotlinScript =
API_URLS.KOTLIN_JS +
`${normalizeJsVersion(this.kotlinVersion)}/kotlin.js`;
iframeDoc.write("<script src='" + kotlinScript + "'></script>");
}
if (
!isWasmRelated(targetPlatform) &&
targetPlatform !== TargetPlatforms.SWIFT_EXPORT
) {
for (let lib of jsLibs) {
iframeDoc.write("<script src='" + lib + "'></script>");
}
if (targetPlatform === TargetPlatforms.JS_IR) {
iframeDoc.write(`<script>${INIT_SCRIPT_IR}</script>`);
} else {
iframeDoc.write(`<script>${INIT_SCRIPT}</script>`);
}
}
if (targetPlatform === TargetPlatforms.COMPOSE_WASM) {
const skikoStdlib = fetch(API_URLS.RESOURCE_VERSIONS(),{
method: 'GET'
}).then(response => response.json())
.then(versions => {
const skikoVersion = versions["skiko"];
const skikoExports = fetch(API_URLS.SKIKO_MJS(skikoVersion), {
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(skikoVersion)}'`
))
.then(skikoCode =>
executeJs(
this.iframe.contentWindow,
skikoCode,
))
.then(skikoExports => fixedSkikoExports(skikoExports))
const stdlibVersion = versions["stdlib"];
const stdlibExports = fetch(API_URLS.STDLIB_MJS(stdlibVersion), {
method: 'GET',
headers: {
'Content-Type': 'text/javascript',
}
}).then(script => script.text())
.then(script =>
// necessary to load stdlib.wasm before its initialization to parallelize
// language=JavaScript
(`const stdlibWasm = fetch('${API_URLS.STDLIB_WASM(stdlibVersion)}');\n` + script).replace(
"fetch(new URL('./stdlib_master.wasm',import.meta.url).href)",
"stdlibWasm"
).replace(
"(extends) => { return { extends }; }",
"(extends_) => { return { extends_ }; }"
))
.then(stdlibCode =>
executeWasmCodeWithSkiko(
this.iframe.contentWindow,
stdlibCode,
)
)
return Promise.all([skikoExports, stdlibExports])
})
this.stdlibExports = skikoStdlib
.then(async ([skikoExportsResult, stdlibExportsResult]) => {
return [
await stdlibExportsResult.instantiate({
"./skiko.mjs": skikoExportsResult
}),
stdlibExportsResult
]
}
)
.then(([stdlibResult, outputResult]) => {
return {
"stdlib": stdlibResult.exports,
"output": outputResult.bufferedOutput
}
}
)
this.iframe.height = "1000"
iframeDoc.write(`<canvas height="1000" id="ComposeTarget"></canvas>`);
}
iframeDoc.write('<body style="margin: 0; overflow: hidden;"></body>');
iframeDoc.close();
}