Skip to content
This repository was archived by the owner on Mar 25, 2019. It is now read-only.

Commit 417d5cc

Browse files
author
Eric Lange
committed
Update README.md
More detailed documentation. Link to Javadocs.
1 parent ff7df07 commit 417d5cc

1 file changed

Lines changed: 66 additions & 8 deletions

File tree

README.md

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,38 @@ Version
2121
-------
2222
2.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
3229
data 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
3431
environments.
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
3738
JSContext 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
3846
context.property("a", 5);
3947
JSValue aValue = context.property("a");
4048
double a = aValue.toNumber();
4149
DecimalFormat df = new DecimalFormat(".#");
4250
System.out.println(df.format(a)); // 5.0
51+
```
52+
53+
You can also run JavaScript code in the context:
54+
55+
```java
4356
context.evaluateScript("a = 10");
4457
JSValue newAValue = context.property("a");
4558
System.out.println(df.format(newAValue.toNumber())); // 10.0
@@ -51,6 +64,48 @@ JSValue fact_a = context.property("fact_a");
5164
System.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+
54109
Building 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

Comments
 (0)