|
| 1 | +# QuickJS Android |
| 2 | + |
| 3 | +> Self-maintained QuickJS Android Bindings. |
| 4 | +
|
| 5 | +## Build |
| 6 | + |
| 7 | +```bash |
| 8 | +git clone --recurse-submodules https://github.com/shiqimei/quickjs-android.git |
| 9 | +``` |
| 10 | + |
| 11 | +Open the folder `quickjs-android` in Android Studio. |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Evaluate Javascript Scripts |
| 16 | + |
| 17 | +```Java |
| 18 | +QuickJS quickJS = new QuickJS.Builder().build(); |
| 19 | +try (JSRuntime runtime = quickJS.createJSRuntime()) { |
| 20 | + try (JSContext context = runtime.createJSContext()) { |
| 21 | + String script1 = "" + |
| 22 | + "function fibonacci(n) {" + |
| 23 | + " if (n == 0 || n == 1) return n;" + |
| 24 | + " return fibonacci(n - 1) + fibonacci(n - 2);" + |
| 25 | + "}"; |
| 26 | + // Evaluate a script without return value |
| 27 | + context.evaluate(script1, "fibonacci.js"); |
| 28 | + |
| 29 | + String script2 = "fibonacci(10);"; |
| 30 | + // Evaluate a script with return value |
| 31 | + int result = context.evaluate(script2, "fibonacci.js", int.class); |
| 32 | + assertEquals(55, result); |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Call Java Methods in Javascript Scripts |
| 38 | + |
| 39 | +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. |
| 40 | + |
| 41 | +```Java |
| 42 | +QuickJS quickJS = new QuickJS.Builder().build(); |
| 43 | +try (JSRuntime runtime = quickJS.createJSRuntime()) { |
| 44 | + try (JSContext context = runtime.createJSContext()) { |
| 45 | + // Non-static method |
| 46 | + Integer integer = 0; |
| 47 | + JSFunction zeroCompareTo = context.createJSFunction(integer, Method.create(Integer.class, Integer.class.getMethod("compareTo", Integer.class))); |
| 48 | + // Add the function to the global object |
| 49 | + context.getGlobalObject().setProperty("zeroCompareTo", zeroCompareTo); |
| 50 | + assertEquals(-1, (int) context.evaluate("zeroCompareTo(1)", "test.js", int.class)); |
| 51 | + assertEquals(1, (int) context.evaluate("zeroCompareTo(-1)", "test.js", int.class)); |
| 52 | + |
| 53 | + // Static method |
| 54 | + JSFunction javaAbs = context.createJSFunctionS(Math.class, Method.create(Math.class, Math.class.getMethod("abs", int.class))); |
| 55 | + // Add the function to the global object |
| 56 | + context.getGlobalObject().setProperty("javaAbs", javaAbs); |
| 57 | + assertEquals(1, (int) context.evaluate("javaAbs(1)", "test.js", int.class)); |
| 58 | + assertEquals(1, (int) context.evaluate("javaAbs(-1)", "test.js", int.class)); |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Or create a JSFunction with a callback. |
| 64 | + |
| 65 | +```Java |
| 66 | +QuickJS quickJS = new QuickJS.Builder().build(); |
| 67 | +try (JSRuntime runtime = quickJS.createJSRuntime()) { |
| 68 | + try (JSContext context = runtime.createJSContext()) { |
| 69 | + // Create a JSFunction with a callback |
| 70 | + JSValue plusFunction = context.createJSFunction((context, args) -> { |
| 71 | + int a = args[0].cast(JSNumber.class).getInt(); |
| 72 | + int b = args[1].cast(JSNumber.class).getInt(); |
| 73 | + int sum = a + b; |
| 74 | + return context.createJSNumber(sum); |
| 75 | + }); |
| 76 | + |
| 77 | + context.getGlobalObject().setProperty("plus", plusFunction); |
| 78 | + int result = context.evaluate("plus(1, 2)", "test.js", Integer.class); |
| 79 | + assertThat(result).isEqualTo(3); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### Call Javascript Methods in Java codes |
| 85 | + |
| 86 | +Just **evaluate** it. Or call `JSFunction.invoke()`. |
| 87 | + |
| 88 | +### Promise |
| 89 | + |
| 90 | +Use `JSContext.executePendingJob()` to execute pending job of promises. You may call `JSContext.executePendingJob()` several times until it returns `false`. |
| 91 | + |
| 92 | +```Java |
| 93 | +QuickJS quickJS = new QuickJS.Builder().build(); |
| 94 | +try (JSRuntime runtime = quickJS.createJSRuntime()) { |
| 95 | + try (JSContext context = runtime.createJSContext()) { |
| 96 | + context.evaluate("a = 1;Promise.resolve().then(() => { a = 2 })", "test.js"); |
| 97 | + assertEquals(1, context.getGlobalObject().getProperty("a").cast(JSNumber.class).getInt()); |
| 98 | + // Execute the pending job |
| 99 | + assertTrue(context.executePendingJob()); |
| 100 | + assertEquals(2, context.getGlobalObject().getProperty("a").cast(JSNumber.class).getInt()); |
| 101 | + // No pending job |
| 102 | + assertFalse(context.executePendingJob()); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Conversion between Java Values and Javascript Values |
| 108 | + |
| 109 | +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. |
| 110 | + |
| 111 | +```Java |
| 112 | +QuickJS quickJS = new QuickJS.Builder().build(); |
| 113 | +try (JSRuntime runtime = quickJS.createJSRuntime()) { |
| 114 | + try (JSContext context = runtime.createJSContext()) { |
| 115 | + String[] result = context.evaluate("['hello', 'world']", "test.js", String[].class); |
| 116 | + assertArrayEquals(new String[] { "hello", "world" }, result); |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +Java Interfaces are also supported. |
| 122 | + |
| 123 | +```Java |
| 124 | +interface Calculator { |
| 125 | + double plus(double a, double b); |
| 126 | + double minus(double a, double b); |
| 127 | + double multiplies(double a, double b); |
| 128 | + double divides(double a, double b); |
| 129 | + void noop(); |
| 130 | +} |
| 131 | + |
| 132 | +QuickJS quickJS = new QuickJS.Builder().build(); |
| 133 | +try (JSRuntime runtime = quickJS.createJSRuntime()) { |
| 134 | + try (JSContext context = runtime.createJSContext()) { |
| 135 | + Calculator calculator = context.evaluate("" + |
| 136 | + "a = {\n" + |
| 137 | + " plus: function(a, b) { return a + b },\n" + |
| 138 | + " minus: function(a, b) { return a - b },\n" + |
| 139 | + " multiplies: function(a, b) { return a * b },\n" + |
| 140 | + " divides: function(a, b) { return a / b },\n" + |
| 141 | + " noop: function() { }\n" + |
| 142 | + "}", "test.js", Calculator.class); |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +Use `TypeAdapter` to support any type you like. |
| 148 | + |
| 149 | +```Java |
| 150 | +private static class AtomicIntegerTypeAdapter extends TypeAdapter<AtomicInteger> { |
| 151 | + @Override |
| 152 | + public JSValue toJSValue(Depot depot, Context context, AtomicInteger value) { |
| 153 | + return context.createJSNumber(value.get()); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public AtomicInteger fromJSValue(Depot depot, Context context, JSValue value) { |
| 158 | + return new AtomicInteger(value.cast(JSNumber.class).getInt()); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +QuickJS quickJS = new QuickJS.Builder().registerTypeAdapter(AtomicInteger.class, new AtomicIntegerTypeAdapter()).build(); |
| 163 | +try (JSRuntime runtime = quickJS.createJSRuntime()) { |
| 164 | + try (JSContext context = runtime.createJSContext()) { |
| 165 | + AtomicInteger atomicInteger = context.evaluate("1", "test.js", AtomicInteger.class); |
| 166 | + assertEquals(1, atomicInteger.get()); |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +## Concept |
| 172 | + |
| 173 | +QuickJS Android uses the similar APIs to QuickJS. |
| 174 | + |
| 175 | +### JSRuntime |
| 176 | + |
| 177 | +> 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. |
| 178 | +> |
| 179 | +> -- QuickJS Document |
| 180 | +
|
| 181 | +### JSContext |
| 182 | + |
| 183 | +> 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. |
| 184 | +> |
| 185 | +> -- QuickJS Document |
| 186 | +
|
| 187 | +### JSValue |
| 188 | + |
| 189 | +> JSValue represents a Javascript value which can be a primitive type or an object. |
| 190 | +> |
| 191 | +> -- QuickJS Document |
| 192 | +
|
| 193 | +Available subclasses of `JSValue` are `JSNull`, `JSUndefined`, `JSBoolean`, `JSNumber`, `JSString`, `JSObject`, `JSArray`, `JSFunction`, `JSSymbol`. |
| 194 | + |
| 195 | +## Test |
| 196 | + |
| 197 | +The original tests and benchmarks of QuickJS are in [android-test](android-test). It's a console-like app running all tests and benchmarks at startup, like `make test`. |
| 198 | + |
| 199 | +## Acknowledgement |
| 200 | + |
| 201 | +1. [bellard/quickjs](https://github.com/bellard/quickjs) QuickJS official repository. |
| 202 | +2. [seven332/quickjs-android](https://github.com/seven332/quickjs-android) QuickJS Android Wrapper. |
| 203 | +3. [openwebf/quickjs](https://github.com/openwebf/quickjs) Optimized quickjs mantained by OpenWebF team. |
| 204 | +4. [taoweiji/quickjs-android](https://github.com/taoweiji/quickjs-android) Android Bindings for QuickJS, A fine little javascript engine. |
0 commit comments