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

Commit 03060c7

Browse files
author
ericwlange
committed
Significant changes to how functions and especially constructors work. Bumping to 3.0.
Deprecated all function methods from JSObject and created a JSFunction subclass. Now all functions can be constructors and simplified the constructor methods. Better alignment between how Java and JavaScript handle instances.
1 parent ef34033 commit 03060c7

11 files changed

Lines changed: 1129 additions & 713 deletions

File tree

AndroidJSCore/AndroidJSCore/src/main/java/org/liquidplayer/webkit/javascriptcore/JSArray.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3232
*/
3333
package org.liquidplayer.webkit.javascriptcore;
3434

35+
import java.util.List;
36+
3537
/**
3638
* A convenience class for handling JavaScript arrays
3739
*

AndroidJSCore/AndroidJSCore/src/main/java/org/liquidplayer/webkit/javascriptcore/JSContext.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3838

3939
import org.liquidplayer.hemroid.JavaScriptCoreGTK;
4040

41+
import java.lang.reflect.Method;
4142
import java.util.HashMap;
4243
import java.util.Map;
4344

@@ -183,16 +184,20 @@ public JSContext(final JSContextGroup inGroup) {
183184
* @since 1.0
184185
* @throws JSException
185186
*/
186-
public JSContext(Class<?> iface) throws JSException {
187+
public JSContext(final Class<?> iface) throws JSException {
187188
mWorker = new JSContextWorker();
188189
context = this;
189190
sync(new Runnable() {
190191
@Override public void run() {
191192
ctx = create();
192193
valueRef = getGlobalObject(ctx);
194+
Method[] methods = iface.getDeclaredMethods();
195+
for (int i=0; i<methods.length; i++) {
196+
JSObject f = new JSFunction(context, methods[i], JSObject.class, context);
197+
property(methods[i].getName(),f);
198+
}
193199
}
194200
});
195-
initJSInterface(this, iface, null);
196201
}
197202
/**
198203
* Creates a JavaScript context in context group 'inGroup', and defines the global object
@@ -203,16 +208,20 @@ public JSContext(Class<?> iface) throws JSException {
203208
* @since 1.0
204209
* @throws JSException
205210
*/
206-
public JSContext(final JSContextGroup inGroup, Class<?> iface) throws JSException {
211+
public JSContext(final JSContextGroup inGroup, final Class<?> iface) throws JSException {
207212
mWorker = new JSContextWorker();
208213
context = this;
209214
sync(new Runnable() {
210215
@Override public void run() {
211216
ctx = createInGroup(inGroup.groupRef());
212217
valueRef = getGlobalObject(ctx);
218+
Method[] methods = iface.getDeclaredMethods();
219+
for (int i=0; i<methods.length; i++) {
220+
JSObject f = new JSFunction(context, methods[i], JSObject.class, context);
221+
property(methods[i].getName(),f);
222+
}
213223
}
214224
});
215-
this.initJSInterface(this, iface, null);
216225
}
217226
@Override
218227
protected void finalize() throws Throwable {
@@ -246,7 +255,7 @@ public void clearExceptionHandler() {
246255
* If an exception handler is set, calls the exception handler, otherwise throws
247256
* the JSException.
248257
* @param exception The JSException to be thrown
249-
* @since 1.0
258+
* @since 2.1
250259
*/
251260
public void throwJSException(JSException exception) throws JSException {
252261
if (exceptionHandler == null) {
@@ -302,7 +311,8 @@ public JSValue evaluateScript(final String script, final JSObject thiz,
302311
JNIReturnClass runnable = new JNIReturnClass() {
303312
@Override public void run() {
304313
jni = evaluateScript(ctx, new JSString(script).stringRef(),
305-
(thiz == null) ? 0L : thiz.valueRef(), (sourceURL == null) ? 0L : new JSString(sourceURL).stringRef(),
314+
(thiz == null) ? 0L : thiz.valueRef(), (sourceURL == null) ? 0L :
315+
new JSString(sourceURL).stringRef(),
306316
startingLineNumber);
307317
}
308318
};
@@ -366,13 +376,19 @@ public synchronized void finalizeObject(JSObject obj) {
366376
* @since 1.0
367377
* @return The JSObject representing the reference
368378
*/
369-
public synchronized JSObject getObjectFromRef(long objRef) {
379+
public synchronized JSObject getObjectFromRef(long objRef,boolean create) {
370380
JSObject obj = objects.get(objRef);
371-
if (obj==null) {
372-
obj = new JSObject(objRef, this);
381+
if (obj==null && create) {
382+
if (isFunction(ctxRef(),objRef))
383+
obj = new JSFunction(objRef,this);
384+
else
385+
obj = new JSObject(objRef, this);
373386
}
374387
return obj;
375388
}
389+
public synchronized JSObject getObjectFromRef(long objRef) {
390+
return getObjectFromRef(objRef,true);
391+
}
376392
/**
377393
* Forces JavaScript garbage collection on this context
378394
* @since 1.0

0 commit comments

Comments
 (0)