Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add playground DCE flag for the JS IR target platform #163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ fun main(args: Array<String>) {

</div>

Use `data-dce` attribute with value `true` to get less code from JS IR target:

<div class="kotlin-code" data-target-platform="js-ir" data-dce="true">

```kotlin
fun mul(a: Int, b: Int): Int {
return a * b
}

fun main(args: Array<String>) {
println(mul(-1, 8))
}
```

</div>


Use `data-target-platform` attribute with value `junit` for creating examples with tests:

Expand Down
16 changes: 15 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@ export const RUNTIME_CONFIG = {...getConfigFromElement(currentScript)};
/**
* API Paths
*
* @type {{COMPILE: string, COMPLETE: string, VERSIONS: string, JQUERY: string, KOTLIN_JS: string}}
* @type {{COMPILE: string, COMPLETE: string, VERSIONS: string, JQUERY: string, KOTLIN_JS: string, WITH_DCE: this}}
*/
export const API_URLS = {
server: RUNTIME_CONFIG.server || __WEBDEMO_URL__,
WITH_DCE(dce) {
const self = this;
return {
...this,
COMPILE(...args) {
let url = self.COMPILE(...args);
if (url.includes("?")) {
url += "&"
}
url += "dce=" + Boolean(dce)
return url
}
};
},
COMPILE(platform, version) {
let url;

Expand Down
4 changes: 2 additions & 2 deletions src/executable-code/executable-fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {

execute() {
const {
onOpenConsole, targetPlatform, waitingForOutput, compilerVersion, onRun, onError,
onOpenConsole, targetPlatform, waitingForOutput, compilerVersion, onRun, onError, dce,
args, theme, hiddenDependencies, onTestPassed, onTestFailed, onCloseConsole, jsLibs, outputHeight, getJsCode
} = this.state;
if (waitingForOutput) {
Expand Down Expand Up @@ -325,7 +325,7 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
)
} else {
this.jsExecutor.reloadIframeScripts(jsLibs, this.getNodeForMountIframe(), targetPlatform);
WebDemoApi.translateKotlinToJs(this.getCode(), compilerVersion, targetPlatform, args, hiddenDependencies).then(
WebDemoApi.translateKotlinToJs(this.getCode(), compilerVersion, targetPlatform, args, hiddenDependencies, { dce }).then(
state => {
state.waitingForOutput = false;
const jsCode = state.jsCode;
Expand Down
3 changes: 3 additions & 0 deletions src/executable-code/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const ATTRIBUTES = {
COMPLETE: 'data-autocomplete',
ON_FLY_HIGHLIGHT: 'highlight-on-fly',
PLATFORM: 'data-target-platform',
DCE: 'data-dce',
JS_LIBS: 'data-js-libs',
FOLDED_BUTTON: 'folded-button',
ARGUMENTS: 'args',
Expand Down Expand Up @@ -95,6 +96,7 @@ export default class ExecutableCode {
const hiddenDependencies = this.getHiddenDependencies(targetNode);
const outputHeight = targetNode.getAttribute(ATTRIBUTES.OUTPUT_HEIGHT) || null;
const targetPlatform = TargetPlatform.getById(targetNode.getAttribute(ATTRIBUTES.PLATFORM));
const dce = targetNode.getAttribute(ATTRIBUTES.DCE) === "true";
const targetNodeStyle = targetNode.getAttribute(ATTRIBUTES.STYLE);
const jsLibs = this.getJsLibraries(targetNode, targetPlatform);
const isFoldedButton = targetNode.getAttribute(ATTRIBUTES.FOLDED_BUTTON) !== "false";
Expand Down Expand Up @@ -168,6 +170,7 @@ export default class ExecutableCode {
onFlyHighLight: onFlyHighLight,
autoIndent: autoIndent,
highlightOnly: highlightOnly,
dce: dce,
targetPlatform: targetPlatform,
jsLibs: jsLibs,
isFoldedButton: isFoldedButton,
Expand Down
12 changes: 10 additions & 2 deletions src/webdemo-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ export default class WebDemoApi {
* @param platform - TargetPlatform
* @param args - command line arguments
* @param hiddenDependencies - read only additional files
* @param compilationOptions - options to handle different kind of compilation modes like DCE, PER MODULE, MINIFIED METHOD AND PROPERTY NAMES
* @returns {*|PromiseLike<T>|Promise<T>}
*/
static translateKotlinToJs(code, compilerVersion, platform, args, hiddenDependencies) {
static translateKotlinToJs(code, compilerVersion, platform, args, hiddenDependencies, compilationOptions = {}) {
const MINIMAL_MINOR_VERSION_IR = 5
if (platform === TargetPlatform.JS_IR && parseInt(compilerVersion.split(".")[1]) < MINIMAL_MINOR_VERSION_IR) {
return Promise.resolve({
Expand All @@ -61,7 +62,14 @@ export default class WebDemoApi {
jsCode: ""
})
} else {
return executeCode(API_URLS.COMPILE(platform, compilerVersion), code, compilerVersion, platform, args, hiddenDependencies).then(function (data) {
return executeCode(
API_URLS.WITH_DCE(compilationOptions.dce).COMPILE(platform, compilerVersion),
code,
compilerVersion,
platform,
args,
hiddenDependencies
).then(function (data) {
let output = "";
let errorsAndWarnings = flatten(Object.values(data.errors));
return {
Expand Down