Skip to content

Commit 623dadb

Browse files
committed
feat: remove deprecated JS platform
1 parent 991bb06 commit 623dadb

File tree

12 files changed

+53485
-136
lines changed

12 files changed

+53485
-136
lines changed

src/config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ export const API_URLS = {
2828
url = `${this.server}/api/${version}/compiler/translate`;
2929
break;
3030
case TargetPlatforms.JS:
31-
url = `${this.server}/api/${version}/compiler/translate`;
32-
break;
33-
case TargetPlatforms.JS_IR:
3431
url = `${this.server}/api/${version}/compiler/translate?ir=true`;
3532
break;
3633
case TargetPlatforms.WASM:

src/executable-code/executable-fragment.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
313313
}
314314

315315
onConsoleCloseButtonEnter() {
316-
const { jsLibs, onCloseConsole, targetPlatform, compilerVersion } = this.state;
316+
const { jsLibs, onCloseConsole, targetPlatform, compilerVersion } =
317+
this.state;
317318
// creates a new iframe and removes the old one, thereby stops execution of any running script
318319
if (isJsRelated(targetPlatform) || isWasmRelated(targetPlatform))
319320
this.jsExecutor.reloadIframeScripts(
@@ -454,6 +455,7 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
454455
theme,
455456
onError,
456457
additionalRequestsResults,
458+
compilerVersion,
457459
)
458460
.then((output) => {
459461
const originState = state.openConsole;

src/js-executor/index.js

Lines changed: 128 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
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';
813

914
const 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
}

src/utils/platforms/TargetPlatforms.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import TargetPlatform from './TargetPlatform';
22

33
export const TargetPlatforms = {
44
JS: new TargetPlatform('js', 'JavaScript'),
5-
JS_IR: new TargetPlatform('js-ir', 'JavaScript IR'),
65
WASM: new TargetPlatform('wasm', 'Wasm'),
76
COMPOSE_WASM: new TargetPlatform('compose-wasm', 'Compose Wasm'),
87
JAVA: new TargetPlatform('java', 'JVM'),

src/utils/platforms/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { TargetPlatforms } from './TargetPlatforms';
33
import { isKeyOfObject } from '../types';
44

55
export function getTargetById(id?: string | null) {
6-
const key = id && id.toUpperCase().replace(/-/g, '_');
6+
let key = id && id.toUpperCase().replace(/-/g, '_');
7+
8+
if (key === 'JS_IR') {
9+
console.warn('JS_IR is deprecated, use JS + compiler-version instead');
10+
key = 'JS';
11+
}
712

813
return isKeyOfObject(key, TargetPlatforms) ? TargetPlatforms[key] : null;
914
}
@@ -17,7 +22,6 @@ export function isJavaRelated(platform: TargetPlatform) {
1722
export function isJsRelated(platform: TargetPlatform) {
1823
return (
1924
platform === TargetPlatforms.JS ||
20-
platform === TargetPlatforms.JS_IR ||
2125
platform === TargetPlatforms.CANVAS ||
2226
platform === TargetPlatforms.SWIFT_EXPORT
2327
);
@@ -30,4 +34,10 @@ export function isWasmRelated(platform: TargetPlatform) {
3034
);
3135
}
3236

37+
const MINIMAL_VERSION_IR = '1.5.0';
38+
39+
export function isJsLegacy(platform: TargetPlatform, compilerVersion: string) {
40+
return isJsRelated(platform) && compilerVersion < MINIMAL_VERSION_IR;
41+
}
42+
3343
export * from './TargetPlatforms';

0 commit comments

Comments
 (0)