execute()

in src/executable-code/executable-fragment.js [342:524]


  execute() {
    const {
      onOpenConsole,
      targetPlatform,
      waitingForOutput,
      compilerVersion,
      onRun,
      onError,
      args,
      theme,
      hiddenDependencies,
      onTestPassed,
      onTestFailed,
      onCloseConsole,
      jsLibs,
      outputHeight,
      getJsCode,
    } = this.state;
    if (waitingForOutput) {
      return;
    }
    this.update({
      shorterHeight: 0,
      waitingForOutput: true,
      openConsole: false,
    });
    if (onOpenConsole) onOpenConsole(); //open when waitingForOutput=true
    if (onRun) onRun();
    if (isJavaRelated(targetPlatform)) {
      WebDemoApi.executeKotlinCode(
        this.getCode(),
        compilerVersion,
        targetPlatform,
        args,
        theme,
        hiddenDependencies,
        onTestPassed,
        onTestFailed,
      ).then(
        (state) => {
          state.waitingForOutput = false;
          if (state.output || state.exception) {
            state.openConsole = true;
          } else {
            if (onCloseConsole) onCloseConsole();
          }
          if ((state.errors.length > 0 || state.exception) && onError)
            onError();
          this.update(state);
        },
        (error) => {
          if (onError) onError();
          this.update({
            waitingForOutput: false,
            output: processErrors([
              {
                severity: 'ERROR',
                message: error.message,
              },
            ]),
            openConsole: true,
            exception: null,
          });
        },
      );
    } else {
      this.jsExecutor.reloadIframeScripts(
        jsLibs,
        this.getNodeForMountIframe(),
        targetPlatform,
        compilerVersion,
      );
      const additionalRequests = [];
      if (targetPlatform === TargetPlatforms.COMPOSE_WASM) {
        if (this.jsExecutor.stdlibExports) {
          additionalRequests.push(this.jsExecutor.stdlibExports);
        }
      }

      Promise.all([
        WebDemoApi.translateKotlinToJs(
          this.getCode(),
          compilerVersion,
          targetPlatform,
          args,
          hiddenDependencies,
        ),
        ...additionalRequests,
      ]).then(
        ([state, ...additionalRequestsResults]) => {
          state.waitingForOutput = false;
          const jsCode = state.jsCode;
          const wasm = state.wasm;
          delete state.jsCode;
          if (getJsCode) getJsCode(jsCode);
          let errors = state.errors.filter(
            (error) => error.severity === 'ERROR',
          );
          if (errors.length > 0) {
            if (onError) onError();
            state.output = processErrors(errors);
            state.openConsole = true;
            state.exception = null;
            this.update(state);
          } else {
            this.jsExecutor
              .executeJsCode(
                jsCode,
                wasm,
                jsLibs,
                targetPlatform,
                outputHeight,
                theme,
                onError,
                additionalRequestsResults,
              )
              .then((output) => {
                const originState = state.openConsole;

                if (
                  targetPlatform === TargetPlatforms.CANVAS ||
                  targetPlatform === TargetPlatforms.COMPOSE_WASM
                ) {
                  state.openConsole = true;
                }

                if (output) {
                  state.openConsole = true;
                  state.output = output;
                } else {
                  state.output = '';
                  if (onCloseConsole) onCloseConsole();
                }

                if (
                  onOpenConsole &&
                  originState !== state.openConsole &&
                  state.openConsole === true
                )
                  onOpenConsole();

                state.exception = null;
                this.update(state);
                /*
                if (targetPlatform === TargetPlatforms.SWIFT_EXPORT) {
                  const code = this.nodes[0]
                    .querySelector(SELECTORS.JS_CODE_OUTPUT_EXECUTOR)
                    .querySelector('.result-code');

                  if (code) {
                    CodeMirror(
                      (elt) => code.parentNode.replaceChild(elt, code),
                      {
                        mode: 'swift',
                        readOnly: 'nocursor',
                        scrollbarStyle: 'native',
                        theme: this.state.theme,
                        value: code.innerText,
                      },
                    );
                  }
                }
                */
              });
          }
        },
        (error) => {
          if (onError) onError();
          this.update({
            waitingForOutput: false,
            output: processErrors([
              {
                severity: 'ERROR',
                message: error.message,
              },
            ]),
            openConsole: true,
            exception: null,
          });
        },
      );
    }
  }