@@ -21,25 +21,38 @@ Version
2121-------
22222.0 (not yet released, but HEAD works)
2323
24- Example
25- -------
26-
27- See Owen Matthew's excellent [ blog post] on the iOS 7 JavaScriptCore framework for an
28- introduction. This example is taken directly from the post. In the included example
29- application, Owen's entire Objective-C tutorial is implemented in Java.
24+ Working With AndroidJSCore
25+ --------------------------
3026
31- Browse the example app source for more detailed examples that cover the basics, sharing
27+ Please see the [ Javadocs] for complete documentation of the API. Also take a look at the
28+ [ example app source code] . It contains more detailed examples that cover the basics, sharing
3229data and functions between Java and JavaScript, wrapping JS classes in Java which
33- are accessible from both environments, and asynchronous, multi-threaded callbacks between
30+ are accessible from both environments, and asynchronous, multi-threaded callbacks between
3431environments.
3532
33+ To get started, you need to create a JavaScript ` JSContext ` . The execution of JS code
34+ occurs within this context, and separate contexts are isolated virtual machines which
35+ do not interact with each other.
36+
3637``` java
3738JSContext context = new JSContext ();
39+ ```
40+
41+ This context is itself a JavaScript object. And as such, you can get and set its properties.
42+ Since this is the global JavaScript object, these properties will be in the top-level
43+ context for all subsequent code in the environment.
44+
45+ ``` java
3846context. property(" a" , 5 );
3947JSValue aValue = context. property(" a" );
4048double a = aValue. toNumber();
4149DecimalFormat df = new DecimalFormat (" .#" );
4250System . out. println(df. format(a)); // 5.0
51+ ```
52+
53+ You can also run JavaScript code in the context:
54+
55+ ``` java
4356context. evaluateScript(" a = 10" );
4457JSValue newAValue = context. property(" a" );
4558System . out. println(df. format(newAValue. toNumber())); // 10.0
@@ -51,6 +64,48 @@ JSValue fact_a = context.property("fact_a");
5164System . out. println(df. format(fact_a. toNumber())); // 3628800.0
5265```
5366
67+ AndroidJSCore is much more powerful than that. You can also write functions in
68+ Java, but expose them to JavaScript:
69+
70+ ``` java
71+ public interface IExposedToJS {
72+ public Integer factorial (Integer x );
73+ }
74+ public class FactorialObject extends JSObject
75+ implements IExposedToJS {
76+ public FactorialObject (JSContext ctx ) {
77+ super (ctx,IExposedToJS . class);
78+ }
79+ @Override
80+ public Integer factorial (Integer x ) {
81+ int factorial = 1 ;
82+ for (; x > 1 ; x-- ) {
83+ factorial *= x;
84+ }
85+ return factorial;
86+ }
87+ }
88+ ```
89+
90+ This class creates a Java object that is also a JavaScript object, which exposes
91+ a single function property ` factorial ` . It can then be passed to the JavaScript
92+ VM:
93+
94+ ``` java
95+ context. property(" myJavaFunctions" , new FactorialObject (context));
96+ context. evaluateScript(" var f = myJavaFunctions.factorial(10);" )
97+ JSValue f = context. property(" f" );
98+ System . out. println(df. format(f. toNumber())); // 3628800.0
99+ ```
100+
101+ If you are used to working with JavaScriptCore in iOS, see the file
102+ [ OwenMatthewsExample.java] in the example app to see side-by-side how to use
103+ AndroidJSCore in Java the same way you would use JavaScriptCore in
104+ Objective-C.
105+
106+ The [ Javadocs] and included example app have detailed descriptions of how to do
107+ just about everything.
108+
54109Building AndroidJSCore-2.0 library
55110-------------------------------
56111
@@ -210,3 +265,6 @@ I am just sticking with Webkit's license, since this thing depends on it.
210265[ latest release ] :https://github.com/ericwlange/AndroidJSCore/releases
211266[ Android Studio ] :http://developer.android.com/sdk/index.html
212267[ webkit ] :https://github.com/ericwlange/webkit
268+ [ Javadocs ] :http://ericwlange.github.io/
269+ [ example app source code ] :https://github.com/ericwlange/AndroidJSCore/tree/master/examples/AndroidJSCoreExample
270+ [ OwenMatthewsExample.java ] :https://github.com/ericwlange/AndroidJSCore/blob/master/examples/AndroidJSCoreExample/app/src/main/java/org/liquidplayer/androidjscoreexample/OwenMatthewsExample.java
0 commit comments