How to access Kotlin object from JavaScript - javascript

I'm using kotlin2js to generate JS library from Kotlin code. I'm then using this library in Javascript (not Kotlin). The code has some Kotlin objects and some normal classes. I can access normal classes from Javascript, but I can't access the objects in any way.
The documentation is pretty sparse, only relevant line might be this:
Kotlin preserves lazy object initialization in JavaScript.
I'm not sure what that means.

I suppose you should specify moduleName as well when accessing from javascript.

The problem was that Kotlin changed name of the method to something like this: calculate_ywek2$(). And it's very hard to figure out, because Kotlin doesn't generate Typescript definitions, so autocomplete doesn't work. The name can be changed with #JsName annotation.

Related

VSCode extension for Javascript types

Is there a VSCode extension that shows data type of a variable (like if we hover above that variable)? Presumably by detecting the type of a variable by looking at its initialization, and maybe even follow subsequent re-assignments? I know that using Typescript will give me great tooling which will do what I am seeking, but I don't want to use Typescript.
No not really, that's the nature of dynamic programming. VS Code itself can sort of guess, but it's mostly based on frequent strings used within the file or directory.
The closest is JSDoc (which has more uses than just types, you should probably already be using this) and indirectly using Typescript in your plain .js files to help VS Code.
At the top of your files add this:
// #ts-check
https://code.visualstudio.com/docs/nodejs/working-with-javascript#_type-checking-javascript

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.
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).
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.

Object oriented keywords in Javascript

I could see interface,extends,declare keywords has been used in java script libraries
lib.d.ts
as we use in Java.
For example
interface HTMLObjectElement extends HTMLElement, GetSVGDocument { }
Why we can't use those keywords when we would like to apply oo design patterns to Javascript appliccations like Node.js
That is a typescript type definition file.
In order to use the same keywords you will need to code your Node.js application on Typescript and have a compiler that transform it into valid javascript.

How to parse a Javascript file and get all the used variables?

I am gathering a lot of Javascript code from untrusted people and have to integrate it in my project. As is is untrusted, I would like to check if it doesn't do something nasty.
My main concern is the variables the code uses.
To check it is OK, I would like to parse all the code and verify the name of the variables. For instance, that all the variables are included in window.sandboxedVariables.
Is it possible to parse a Javascript code (in any language but preferably Javascript or bash) and get the list of all the variables ? Is it possible to do the same with the imported libraries ?
Is it possible to do with Uglify ? I read a bit the API documentation and found nothing specific.
Thank you very much !
Assuming you're talking about global variables, you can do the following:
clone the window object
load/run the untrusted script
compare the window object to the cloned one
move all newfound items into window.sandboxedVariables
However, this won't work if the untrusted script overrides one of the existing properties (variables) of window.
eslint is a JavaScript source code linting tool that lets you write custom plugins. You should be able to write a plugin that meets your needs. Plus, the plugins can be written in JavaScript.
http://eslint.org/docs/developer-guide
It is impossible to write an algorithm that verifies untrusted JavaScript code. You can parse it, you can run it in a sandbox and analyze its actions. But you can never be sure you've identified everything it might do or every variable it might use once you run it in your real environment.
If you don't trust it then either only run it in a secure sandbox or don't use it.
You could use Mozilla Rhino. It is a JavaScript engine written in Java.
Here you can find an example similar to what you are trying to do:
http://ramkulkarni.com/blog/parsing-javascript-code-using-mozilla-rhino/

Typescript specific methods for saving data online?

I'm thinking about using TypeScript to create an online app, and this will require saving of data online.
What are my options in regards to that?
Is there anything specific to using TypeScript which makes that easier or harder?
Ideally I would use a service like Parse.com to save data, can Typescript be connected to Parse or would I have to rely upon plain JS?
TypeScript runs wherever javascript runs. So
Your options are the same as javascript.
Typescript compiles down to javascript. And it is designed to be a superset of javascript so your javascript will be valid typescript as long as you have variables declared and sometimes types mentioned.
Optional static typing + easier syntax is what makes developing in TypeScript easier.
Static typing makes refactoring and intellisense more reliable. Having an easier syntax for classes / modules means you are more likely to structure your code better.
Yes you can use parse.com with typescript
The recommended way to do that is to create a declaration file describing your javascript code. In the beginning it can be as simple as:
declare var parse:any;
I wrote some guidance on the matter here : http://basarat.github.io/TypeScriptDeepDive/#/declarations
There is a huge resource of declaration files you can find at https://github.com/borisyankov/DefinitelyTyped . In particular check out FireBase : https://www.firebase.com/ and its declaration file : https://github.com/borisyankov/DefinitelyTyped/tree/master/firebase However there isn't one on parse.com yet which is why I mentioned the way to write your own.
Additionally you don't need a declaration file if you do not want any impressive static checking of the typescript code that interacts with the parse.com's api.

Categories