Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call Java functions from duktape #29

Closed
josdejong opened this issue Dec 1, 2015 · 12 comments
Closed

Call Java functions from duktape #29

josdejong opened this issue Dec 1, 2015 · 12 comments

Comments

@josdejong
Copy link

It would be useful to be able to call native Java/Android functions from within JavaScript, similar to how Rhino allows importing native functions.

For my specific case there is no need to implement full support for conversion between Java and JS objects, it would be enough to just be able to call a function with primitive arguments like double, String, int.

@JakeWharton
Copy link
Member

I was messing with this a while ago. I wrote an API but not an implementation 😀

interface Math {
  int add(int one, int two);
}

// Bind a JS object to Java API.
Math math = duktape.proxy("math", Math.class);
class Listener {
  void onSomething(String result) {
    System.out.println(result);
  }
}

// Bind a Java instance to a JS object.
duktape.bind("listener", new Listener());

@swankjesse
Copy link
Contributor

This looks quite fun.

@josdejong
Copy link
Author

That looks awesome! And much simpler than the API of Rhino.

@josdejong
Copy link
Author

Any news on this?

@ghost
Copy link

ghost commented Feb 2, 2016

I'm hoping to get to this soon, likely in the next week or two.

@josdejong
Copy link
Author

That's good news, thanks for the update!

@ghost
Copy link

ghost commented Mar 20, 2016

This has been implemented!

@ghost ghost closed this as completed Mar 20, 2016
@josdejong
Copy link
Author

Ah, that's cool! Thanks. Going to give it a try soon.

I found some example usages in the unit tests, looks easy to use. A bit of docs/examples would be welcome though.

@ghost
Copy link

ghost commented Mar 20, 2016

Absolutely, I'm updating the README next..

@josdejong josdejong changed the title Call a Java functions from duktape Call Java functions from duktape Apr 3, 2016
@josdejong
Copy link
Author

I just fiddled around with this new feature, it works great, also gives back useful error stack traces. I really love the minimalist interface. Thanks!

Just as a side note: I worked a lot with rhino, which supports not only primitives but also passing Java classes as arguments and return types. In practice this works really painful though, and we ended up passing objects as stringified JSON. So... please just stick to support for primitives :).

One question: what is the reason that you only support a class interface? It would be convenient if you can also just pass a class I think.

@swankjesse
Copy link
Contributor

We use an interface to discourage users from passing arbitrary types like ArrayList or BigInteger. We want the types shared between Java and JavaScript to be designed specifically for that purpose.

@josdejong
Copy link
Author

That makes sense, thanks for the explanation.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants