soywiz-archive / jtransc

Bytecode to source converting Java & Kotlin code into JavaScript, C++, D, C#, PHP, AS3, Dart and Haxe and run it everywhere. Also use JVM code in your favourite language as a library.
https://jtransc.soywiz.com/
Apache License 2.0
632 stars 67 forks source link

javascript to java conversion possible ? #305

Closed eix128 closed 3 years ago

eix128 commented 3 years ago

Hi , is it possible to use javascript library in Java applications? There are lots of growing javascript libraries avalible.I want to use them in my current Java application without any Runtime.

does jtransc supports javascript to java translation ?

soywiz commented 3 years ago

JTransc doesn't support JavaScript to Java translation. I'm afraid, not even related and not its focus.

If you want polyglot development you might want to take a look to GraalVM: https://www.graalvm.org/ you should be able to mix languages with that one.

The focus of JTransc is JVM bytecode (mostly Kotlin) into other languages with an AOT approach in mind like CLI or games, more than using it with dynamic languages like JS. GraalVM or Kotlin multiplatform right now AFAIK don't generate C++ source for example that could allow to target non-standard platforms like homebrew consoles. When JTransc started GraalVM didn't exist, nor Kotlin/Native.

eix128 commented 3 years ago

I know GraalVM but GraalVM doesnt generate javascript library to java wrapper.

For example on here : https://blog.yuzutech.fr/blog/java-api-on-javascript-lib-graalvm/

chroma.min.js library will be used on java But author must write the wrapper manually to use in java directly. This is something like JNI for C++ that we need to write wrap manually all functions.

package org.chroma;

import org.graalvm.polyglot.Value;

import java.io.IOException; import java.net.URISyntaxException; import java.util.List;

public interface Chroma {

Chroma darken();

Chroma saturate(int value);

String hex();

List rgba();

static Chroma create(String color) { try { Value chroma = GraalVMChroma.create().getChroma(); Value instance = chroma.newInstance(color); return new ChromaInstance(instance); } catch (IOException | URISyntaxException e) { throw new RuntimeException("Unable to instantiate Chroma GraalVM"); } } }

and this class written manually:

package org.chroma;

import org.graalvm.polyglot.Value;

import java.util.ArrayList; import java.util.List;

public class ChromaInstance implements Chroma {

private Value chromaInstance;

public ChromaInstance(Value chromaInstance) { this.chromaInstance = chromaInstance; }

@Override public Chroma darken() { return new ChromaInstance(chromaInstance.getMember("darken").execute()); }

@Override public Chroma saturate(int value) { return new ChromaInstance(chromaInstance.getMember("saturate").execute(value)); }

@Override public String hex() { return chromaInstance.getMember("hex").execute().asString(); }

@Override public List rgba() { List result = new ArrayList<>(); Value value = chromaInstance.getMember("rgba").execute(); if (value.hasArrayElements()) { for (int i = 0; i < value.getArraySize(); i++) { result.add(value.getArrayElement(i).asInt()); } } return result; } }

I want these classes generated automatically.So i blindly use js library inside java.

soywiz commented 3 years ago

It looks like a lot of pain to have to implement that yourself. But well, JTransc is not intended for that. JTransc includes code to handle JVM bytecode, not to parse JS or TypeScript definitions.

So I'm afraid, you will have to search elsewhere for that.

Going to close the issue since this is not possible.