https://kotlinlang.org logo
#javascript
Title
# javascript
k

kang wang

03/07/2023, 3:04 AM
I'm using kotlin/js toolchain to compile kotlin into javascript, using the ir based approach. However, I find that even i don't write anything in my main function, the kotlin/js will also generate a kotlin-kotlin-stdlib-js-ir.js of 86KB. The typical codes like:
Copy code
//region block: polyfills
if (typeof ArrayBuffer.isView === 'undefined') {
  ArrayBuffer.isView = function (a) {
    return a != null && a.__proto__ != null && a.__proto__.__proto__ === Int8Array.prototype.__proto__;
  };
}
if (typeof Math.imul === 'undefined') {
  Math.imul = function imul(a, b) {
    return (a & 4.29490176E9) * (b & 65535) + (a & 65535) * (b | 0) | 0;
  };
}
//endregion
(function (root, factory) {
  if (typeof define === 'function' && define.amd)
    define(['exports'], factory);
  else if (typeof exports === 'object')
    factory(module.exports);
  else
    root['kotlin-kotlin-stdlib-js-ir'] = factory(typeof this['kotlin-kotlin-stdlib-js-ir'] === 'undefined' ? {} : this['kotlin-kotlin-stdlib-js-ir']);
}(this, function (_) {
  'use strict';
  //region block: imports
  var imul = Math.imul;
  var isView = ArrayBuffer.isView;
  //endregion
  //region block: pre-declaration
  AbstractMutableCollection.prototype = Object.create(AbstractCollection.prototype);
  AbstractMutableCollection.prototype.constructor = AbstractMutableCollection;
  AbstractMutableList.prototype = Object.create(AbstractMutableCollection.prototype);
  AbstractMutableList.prototype.constructor = AbstractMutableList;
  ArrayList.prototype = Object.create(AbstractMutableList.prototype);
  ArrayList.prototype.constructor = ArrayList;
  NodeJsOutput.prototype = Object.create(BaseOutput.prototype);
  NodeJsOutput.prototype.constructor = NodeJsOutput;
  BufferedOutput.prototype = Object.create(BaseOutput.prototype);
  BufferedOutput.prototype.constructor = BufferedOutput;
  BufferedOutputToConsoleLog.prototype = Object.create(BufferedOutput.prototype);
  BufferedOutputToConsoleLog.prototype.constructor = BufferedOutputToConsoleLog;
  PrimitiveKClassImpl.prototype = Object.create(KClassImpl.prototype);
  PrimitiveKClassImpl.prototype.constructor = PrimitiveKClassImpl;
  NothingKClassImpl.prototype = Object.create(KClassImpl.prototype);
  NothingKClassImpl.prototype.constructor = NothingKClassImpl;
  SimpleKClassImpl.prototype = Object.create(KClassImpl.prototype);
  SimpleKClassImpl.prototype.constructor = SimpleKClassImpl;
  Long.prototype = Object.create(Number_0.prototype);
  Long.prototype.constructor = Long;
  Exception.prototype = Object.create(Error.prototype);
  Exception.prototype.constructor = Exception;
  RuntimeException.prototype = Object.create(Exception.prototype);
  RuntimeException.prototype.constructor = RuntimeException;
  IllegalArgumentException.prototype = Object.create(RuntimeException.prototype);
  IllegalArgumentException.prototype.constructor = IllegalArgumentException;
  IllegalStateException.prototype = Object.create(RuntimeException.prototype);
  IllegalStateException.prototype.constructor = IllegalStateException;
  NoSuchElementException.prototype = Object.create(RuntimeException.prototype);
  NoSuchElementException.prototype.constructor = NoSuchElementException;
  UnsupportedOperationException.prototype = Object.create(RuntimeException.prototype);
  UnsupportedOperationException.prototype.constructor = UnsupportedOperationException;
  IndexOutOfBoundsException.prototype = Object.create(RuntimeException.prototype);
  IndexOutOfBoundsException.prototype.constructor = IndexOutOfBoundsException;
  ClassCastException.prototype = Object.create(RuntimeException.prototype);
  ClassCastException.prototype.constructor = ClassCastException;
  //endregion
  function joinToString(_this__u8e3s4, separator, prefix, postfix, limit, truncated, transform) {
    return joinTo(_this__u8e3s4, StringBuilder_init_$Create$(), separator, prefix, postfix, limit, truncated, transform).toString();
  }
  function joinToString$default(_this__u8e3s4, separator, prefix, postfix, limit, truncated, transform, $mask0, $handler) {
    if (!(($mask0 & 1) === 0))
      separator = ', ';
    if (!(($mask0 & 2) === 0))
      prefix = '';
    if (!(($mask0 & 4) === 0))
      postfix = '';
    if (!(($mask0 & 8) === 0))
      limit = -1;
    if (!(($mask0 & 16) === 0))
      truncated = '...';
    if (!(($mask0 & 32) === 0))
      transform = null;
    return joinToString(_this__u8e3s4, separator, prefix, postfix, limit, truncated, transform);
  }
  function joinTo(_this__u8e3s4, buffer, separator, prefix, postfix, limit, truncated, transform) {
    buffer.a(prefix);
    var count = 0;
    var indexedObject = _this__u8e3s4;
    var inductionVariable = 0;
    var last = indexedObject.length;
    $l$loop: while (inductionVariable < last) {
      var element = indexedObject[inductionVariable];
      inductionVariable = inductionVariable + 1 | 0;
      count = count + 1 | 0;
      if (count > 1) {
        buffer.a(separator);
      }
      if (limit < 0 ? true : count <= limit) {
        appendElement(buffer, element, transform);
      } else
        break $l$loop;
    }
    if (limit >= 0 ? count > limit : false) {
      buffer.a(truncated);
    }
    buffer.a(postfix);
    return buffer;
  }
  function joinToString_0(_this__u8e3s4, separator, prefix, postfix, limit, truncated, transform) {
    return joinTo_0(_this__u8e3s4, StringBuilder_init_$Create$(), separator, prefix, postfix, limit, truncated, transform).toString();
  }
  function joinToString$default_0(_this__u8e3s4, separator, prefix, postfix, limit, truncated, transform, $mask0, $handler) {
    if (!(($mask0 & 1) === 0))
      separator = ', ';
    if (!(($mask0 & 2) === 0))
      prefix = '';
    if (!(($mask0 & 4) === 0))
      postfix = '';
    if (!(($mask0 & 8) === 0))
      limit = -1;
    if (!(($mask0 & 16) === 0))
      truncated = '...';
    if (!(($mask0 & 32) === 0))
      transform = null;
    return joinToString_0(_this__u8e3s4, separator, prefix, postfix, limit, truncated, transform);
  }
  function joinTo_0(_this__u8e3s4, buffer, separator, prefix, postfix, limit, truncated, transform) {
    buffer.a(prefix);
    var count = 0;
    var tmp0_iterator = _this__u8e3s4.b();
    $l$loop: while (tmp0_iterator.c()) {
      var element = tmp0_iterator.d();
      count = count + 1 | 0;
      if (count > 1) {
        buffer.a(separator);
      }
      if (limit < 0 ? true : count <= limit) {
        appendElement(buffer, element, transform);
      } else
        break $l$loop;
    }
    if (limit >= 0 ? count > limit : false) {
      buffer.a(truncated);
    }
    buffer.a(postfix);
    return buffer;
  }
  function getOrElse(_this__u8e3s4, index, defaultValue) {
    return (index >= 0 ? index <= get_lastIndex(_this__u8e3s4) : false) ? charSequenceGet(_this__u8e3s4, index) : defaultValue(index).e_1;
  }
  function get_code(_this__u8e3s4) {
    return Char__toInt_impl_vasixd(_this__u8e3s4);
  }
  function Char(code) {
    var tmp;
    var tmp$ret$0;
    // Inline function 'kotlin.code' call
    Companion_getInstance_0();
    var tmp0__get_code__88qj9g = _Char___init__impl__6a9atx(0);
    tmp$ret$0 = Char__toInt_impl_vasixd(tmp0__get_code__88qj9g);
    if (code < tmp$ret$0) {
      tmp = true;
    } else {
      var tmp$ret$1;
      // Inline function 'kotlin.code' call
      Companion_getInstance_0();
      var tmp1__get_code__adl84j = _Char___init__impl__6a9atx(65535);
      tmp$ret$1 = Char__toInt_impl_vasixd(tmp1__get_code__adl84j);
      tmp = code > tmp$ret$1;
    }
    if (tmp) {
      throw IllegalArgumentException_init_$Create$('Invalid Char code: ' + code);
    }
    return numberToChar(code);
  }
  function AbstractCollection$toString$lambda(this$0) {
    return function (it) {
      return it === this$0 ? '(this Collection)' : toString_0(it);
    };
  }
  function AbstractCollection() {
  }
  AbstractCollection.prototype.toString = function () {
    return joinToString$default_0(this, ', ', '[', ']', 0, null, AbstractCollection$toString$lambda(this), 24, null);
  };
  AbstractCollection.prototype.toArray = function () {
    return copyToArrayImpl(this);
  };
  AbstractCollection.$metadata$ = classMeta('AbstractCollection', [Collection]);
  function Companion() {
    Companion_instance = this;
  }
  Companion.prototype.g = function (index, size) {
    if (index < 0 ? true : index >= size) {
      throw IndexOutOfBoundsException_init_$Create$('index: ' + index + ', size: ' + size);
    }
  };
  Companion.prototype.h = function (c, other) {
    if (!(c.f() === other.f()))
      return false;
    var otherIterator = other.b();
    var tmp0_iterator = c.b();
    while (tmp0_iterator.c()) {
      var elem = tmp0_iterator.d();
      var elemOther = otherIterator.d();
      if (!equals(elem, elemOther)) {
        return false;
      }
    }
    return true;
  };
  Companion.$metadata$ = objectMeta('Companion');
  var Companion_instance;
  function Companion_getInstance() {
    if (Companion_instance == null)
      new Companion();
    return Companion_instance;
  }
  function mutableListOf() {
    return ArrayList_init_$Create$();
  }
  function contract(builder) {
  }
  function KClassifier() {
  }
  KClassifier.$metadata$ = interfaceMeta('KClassifier');
  function appendElement(_this__u8e3s4, element, transform) {
    if (!(transform == null)) {
      _this__u8e3s4.a(transform(element));
    } else {
      if (element == null ? true : isCharSequence(element)) {
        _this__u8e3s4.a(element);
      } else {
        if (element instanceof Char_0) {
          _this__u8e3s4.i(element.e_1);
        } else {
          _this__u8e3s4.a(toString_0(element));
        }
      }
    }
  }
  ...
Is it reasonable? From my point of view, the DCE mechanism should guarantee that only codes used(directly or indirectly) in my main function would appear in the final kotlin-kotlin-stdlib-js-ir.js. Anyone knows why and is it possible for me to minimize the kotlin-kotlin-stdlib-js-ir.js furthur?
1
t

turansky

03/07/2023, 1:27 PM
println
->
console.log
?
z

Zachary Siegel

03/07/2023, 3:45 PM
looks like it’s just pre-defining global polyfills
n

Norbi

03/11/2023, 8:21 PM
What about the optimized production code? Does it have this problem, too?
25 Views