seven332 / quickjs-android

QuickJS Android wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any plan about execute js in java like J2V8 did?

ddrandy opened this issue · comments

commented

I noticed some unit tests in module library and it lacks of something like function calls. Hope you could enhance it. I will appreciative for your help.

Something like this?

QuickJS quickJS = new QuickJS.Builder().build();
try (JSRuntime runtime = quickJS.createJSRuntime()) {
  try (JSContext context = runtime.createJSContext()) {
    String script = "" +
        "function fibonacci(n) {" +
        "  if (n == 0 || n == 1) return n;" +
        "  return fibonacci(n - 1) + fibonacci(n - 2);" +
        "}" +
        "fibonacci(10);";
    int result = context.evaluate(script, "fibonacci.js", int.class);
    assertEquals(55, result);
  }
}
commented

TYVM.
Is there a java class that could represent JSObject or JSArray?

commented

nevermind, I found it.

It's still at an early development stage. Basically, I plan to provide APIs like Gson/Moshi to convert between JavaScript value and Java value via TypeAdapter.

I believe it's done. Feel free to reopen it.