• Stars
    star
    177
  • Rank 215,985 (Top 5 %)
  • Language
    C
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

QuickJS Android wrapper

QuickJS Android

QuickJS Android wrapper.

Build

git clone --recurse-submodules https://github.com/seven332/quickjs-android.git

Open the folder quickjs-android in Android Studio.

Download

  1. Add the JitPack repository to your root build.gradle.
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add quickjs-android dependency to your application build.gradle.
dependencies {
    implementation "com.github.seven332:quickjs-android:0.1.0"
}

Usage

Evaluate Javascript Scripts

QuickJS quickJS = new QuickJS.Builder().build();
try (JSRuntime runtime = quickJS.createJSRuntime()) {
  try (JSContext context = runtime.createJSContext()) {
    String script1 = "" +
        "function fibonacci(n) {" +
        "  if (n == 0 || n == 1) return n;" +
        "  return fibonacci(n - 1) + fibonacci(n - 2);" +
        "}";
    // Evaluate a script without return value
    context.evaluate(script1, "fibonacci.js");

    String script2 = "fibonacci(10);";
    // Evaluate a script with return value
    int result = context.evaluate(script2, "fibonacci.js", int.class);
    assertEquals(55, result);
  }
}

Call Java Methods in Javascript Scripts

Non-static methods and static methods are supported. Wrap a Java method as a JSFunction, then add the JSFunction to the JSContext. Call it like a normal Javascript function.

QuickJS quickJS = new QuickJS.Builder().build();
try (JSRuntime runtime = quickJS.createJSRuntime()) {
  try (JSContext context = runtime.createJSContext()) {
    // Non-static method
    Integer integer = 0;
    JSFunction zeroCompareTo = context.createJSFunction(integer, Method.create(Integer.class, Integer.class.getMethod("compareTo", Integer.class)));
    // Add the function to the global object
    context.getGlobalObject().setProperty("zeroCompareTo", zeroCompareTo);
    assertEquals(-1, (int) context.evaluate("zeroCompareTo(1)", "test.js", int.class));
    assertEquals(1, (int) context.evaluate("zeroCompareTo(-1)", "test.js", int.class));

    // Static method
    JSFunction javaAbs = context.createJSFunctionS(Math.class, Method.create(Math.class, Math.class.getMethod("abs", int.class)));
    // Add the function to the global object
    context.getGlobalObject().setProperty("javaAbs", javaAbs);
    assertEquals(1, (int) context.evaluate("javaAbs(1)", "test.js", int.class));
    assertEquals(1, (int) context.evaluate("javaAbs(-1)", "test.js", int.class));
  }
}

Or create a JSFunction with a callback.

QuickJS quickJS = new QuickJS.Builder().build();
try (JSRuntime runtime = quickJS.createJSRuntime()) {
  try (JSContext context = runtime.createJSContext()) {
    // Create a JSFunction with a callback
    JSValue plusFunction = context.createJSFunction((context, args) -> {
      int a = args[0].cast(JSNumber.class).getInt();
      int b = args[1].cast(JSNumber.class).getInt();
      int sum = a + b;
      return context.createJSNumber(sum);
    });

    context.getGlobalObject().setProperty("plus", plusFunction);
    int result = context.evaluate("plus(1, 2)", "test.js", Integer.class);
    assertThat(result).isEqualTo(3);
  }
}

Call Javascript Methods in Java codes

Just evaluate it. Or call JSFunction.invoke().

Promise

Use JSContext.executePendingJob() to execute pending job of promises. You may call JSContext.executePendingJob() several times until it returns false.

QuickJS quickJS = new QuickJS.Builder().build();
try (JSRuntime runtime = quickJS.createJSRuntime()) {
  try (JSContext context = runtime.createJSContext()) {
    context.evaluate("a = 1;Promise.resolve().then(() => { a = 2 })", "test.js");
    assertEquals(1, context.getGlobalObject().getProperty("a").cast(JSNumber.class).getInt());
    // Execute the pending job
    assertTrue(context.executePendingJob());
    assertEquals(2, context.getGlobalObject().getProperty("a").cast(JSNumber.class).getInt());
    // No pending job
    assertFalse(context.executePendingJob());
  }
}

Conversion between Java Values and Javascript Values

Java values are converted to Javascript values when calling Java methods in Javascript scripts. Javascript values are converted to a Java values when receiving return values from evaluated Javascript scripts. QuickJS Android supports primitive types, string, array.

QuickJS quickJS = new QuickJS.Builder().build();
try (JSRuntime runtime = quickJS.createJSRuntime()) {
  try (JSContext context = runtime.createJSContext()) {
    String[] result = context.evaluate("['hello', 'world']", "test.js", String[].class);
    assertArrayEquals(new String[] { "hello", "world" }, result);
  }
}

Java Interfaces are also supported.

interface Calculator {
  double plus(double a, double b);
  double minus(double a, double b);
  double multiplies(double a, double b);
  double divides(double a, double b);
  void noop();
}

QuickJS quickJS = new QuickJS.Builder().build();
try (JSRuntime runtime = quickJS.createJSRuntime()) {
  try (JSContext context = runtime.createJSContext()) {
    Calculator calculator = context.evaluate("" +
      "a = {\n" +
      "  plus: function(a, b) { return a + b },\n" +
      "  minus: function(a, b) { return a - b },\n" +
      "  multiplies: function(a, b) { return a * b },\n" +
      "  divides: function(a, b) { return a / b },\n" +
      "  noop: function() { }\n" +
      "}", "test.js", Calculator.class);
  }
}

Use TypeAdapter to support any type you like.

private static class AtomicIntegerTypeAdapter extends TypeAdapter<AtomicInteger> {
  @Override
  public JSValue toJSValue(Depot depot, Context context, AtomicInteger value) {
    return context.createJSNumber(value.get());
  }

  @Override
  public AtomicInteger fromJSValue(Depot depot, Context context, JSValue value) {
    return new AtomicInteger(value.cast(JSNumber.class).getInt());
  }
}

QuickJS quickJS = new QuickJS.Builder().registerTypeAdapter(AtomicInteger.class, new AtomicIntegerTypeAdapter()).build();
try (JSRuntime runtime = quickJS.createJSRuntime()) {
  try (JSContext context = runtime.createJSContext()) {
    AtomicInteger atomicInteger = context.evaluate("1", "test.js", AtomicInteger.class);
    assertEquals(1, atomicInteger.get());
  }
}

Concept

QuickJS Android uses the similar APIs to QuickJS.

JSRuntime

JSRuntime represents a Javascript runtime corresponding to an object heap. Several runtimes can exist at the same time but they cannot exchange objects. Inside a given runtime, no multi-threading is supported.

-- QuickJS Document

JSContext

JSContext represents a Javascript context (or Realm). Each JSContext has its own global objects and system objects. There can be several JSContexts per JSRuntime and they can share objects, similar to frames of the same origin sharing Javascript objects in a web browser.

-- QuickJS Document

JSValue

JSValue represents a Javascript value which can be a primitive type or an object.

-- QuickJS Document

Available subclasses of JSValue are JSNull, JSUndefined, JSBoolean, JSNumber, JSString, JSObject, JSArray, JSFunction, JSSymbol.

Test

The original tests and benchmarks of QuickJS are in android-test. It's a console-like app running all tests and benchmarks at startup, like make test.

More Repositories

1

EhViewer

[DEPRECATED] An Unofficial E-Hentai Application for Android
Java
8,512
star
2

Nimingban

[DEPRECATED] A Nimingban Client
Java
309
star
3

a7zip

An Android wrapper for 7-Zip (P7ZIP)
C++
41
star
4

UniFile

[library] Forked from DocumentFile, but more powerful
Java
25
star
5

Traceroute-for-Android

Make https://sourceforge.net/projects/traceroute/ work on Android
C
17
star
6

AC-Battery

Android AC niang emoji battery widget
Java
11
star
7

DrawerLayout

[library] Full-Screen DrawerLayout in Android
Java
10
star
8

RefreshLayout

[library] The user can refresh data or load more data via a vertical swipe gesture
Java
9
star
9

Stage

A View-based Android UI framework
Java
9
star
10

conductor-dialog

A dialog-like Controller
Java
8
star
11

VectorOld

[DEPRECATED]A backport of VectorDrawable, AnimatedVectorDrawable and PathInterpolator
Java
7
star
12

Image

[library] Images with data stored in native heap
C
5
star
13

AndroidChineseString

This is a Android Studio/ IntelliJ IDEA plugin to convert the strings.xml between Chinese
Java
5
star
14

android-recaptcha

Java
4
star
15

android-gallery

Android Gallery Widget
Java
4
star
16

LargeImageView

[library] Display large image on Android
Java
4
star
17

panda-sheriff-bot

Kotlin
3
star
18

Ripple

[library] RippleDrawable for pre-L
Java
3
star
19

android-dialog

DialogView and add-ons
Java
3
star
20

js-runtime-jvm

Kotlin
3
star
21

Yorozuya

[library]
Java
3
star
22

GLGallery

Java
3
star
23

android-chinese-string-gradle-plugin

Java
2
star
24

quickjs

C
2
star
25

EasyRecyclerView

[library] Make RecyclerView easier to use
Java
2
star
26

turing-machine-simulator

Turing Machine Simulator
JavaScript
2
star
27

Conaco

[library]
Java
2
star
28

SwipeBack

[library] Swipe to finish Activity
Java
2
star
29

check-android-binaries

Check for Android
C
2
star
30

conductor-attacher

Java
2
star
31

Hotspot

[library] Get clicking position precisely in Android
Java
2
star
32

kotlin-compatibility-test

Kotlin-based API backward binary compatibility test
Kotlin
2
star
33

android-animator

Java
2
star
34

CardSalon

[DEPRECATED]
Java
1
star
35

BeerBelly

[library]
Java
1
star
36

GLView

Java
1
star
37

fresco-large

Java
1
star
38

seven332.github.io

SCSS
1
star