Search code examples
javascriptnode.jskotlinactions-on-googlekotlin-js-interop

Is @JsName annotation required for every method?


I was trying to make NodeJs work with Kotlin for a HelloWorld example here.

As per the Kotlin JS documentation, @JsName annotation is required for overloaded methods. But in my experience, it is required even for a single method. Without this annotation the compiler adds a suffix to the method name as shown in the screenshot.enter image description here

Is this a bug? Or am I missing something?

I'm using Kotlin 1.1.0 module provided by NPM (please check the GitHub link above for the complete codebase if required).


Solution

  • Kotlin compiler mangles names all functions, except for those which don't take any parameters. The motivation is: you can add overloaded function later, and this should not break binary compatibility of the code. As for @JsName: it depends on your goal. I don't know it and hence I can't tell whether you shuold put @JsName annotation on each method. If you are developing a library which is intended to be used from JavaScript, yes, you probably need to put @JsName on each function you want to be accessible from JavaScript. We are going to add another annotation which turns off mangling on entire class or file.