Skip to content

Commit

Permalink
Merge pull request #6 from taoweiji/develop
Browse files Browse the repository at this point in the history
addJavascriptInterface
  • Loading branch information
taoweiji authored Jun 1, 2021
2 parents 12c1e59 + 75c539f commit 9bb1a44
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 3 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ quickjs-android 是 QuickJS JavaScript 引擎的 Android 接口框架,整体
##### 引入依赖

```groovy
implementation 'io.github.taoweiji.quickjs:quickjs-android:1.1.3'
implementation 'io.github.taoweiji.quickjs:quickjs-android:1.1.4'
```

##### 简单示例
Expand Down Expand Up @@ -87,7 +87,41 @@ context.set("console", new JSObject(context).set("log", log).set("message", mess
context.executeVoidScript("console.log(console.message())", null);
```

##### addJavascriptInterface

```java
public class Console {
int count = 0;

@JavascriptInterface
public void log(String msg) {
count++;
Log.d("console", msg);
}

@JavascriptInterface
public void info(String msg) {
count++;
Log.i("console", msg);
}

@JavascriptInterface
public void error(String msg) {
count++;
Log.e("console", msg);
}

@JavascriptInterface
public int count() {
return count;
}
}

context.addJavascriptInterface(new Console(), "console");
context.executeVoidScript("console.log('Hello World')", null);
int count = context.executeIntegerScript("console.count()", null);
Log.d("console", String.valueOf(count));
```

#### QuickJS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;

Expand Down Expand Up @@ -35,11 +37,12 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onClick(View v) {
for (int i = 0; i < 10000; i++) {
quickJS.createContext();
// quickJS.createContext();
}
}
});
test();
addJavascriptInterface();
}

void test() {
Expand All @@ -58,6 +61,46 @@ public void invoke(JSObject receiver, JSArray args) {
void testV8() {
// V8 v8 = V8.createV8Runtime();
// v8.executeVoidScript("a.a");
WebView webView = new WebView(this);
// @JavascriptInterface
// webView.addJavascriptInterface();
}

public void addJavascriptInterface() {
JSContext context = quickJS.createContext();
context.addJavascriptInterface(new Console(), "console");
context.executeVoidScript("console.log('Hello World')", null);
context.executeVoidScript("console.error('Hello World')", null);
context.executeVoidScript("console.info('Hello World')", null);
int count = context.executeIntegerScript("console.count()", null);
Log.d("console", String.valueOf(count));
}

public static class Console {
int count = 0;

@JavascriptInterface
public void log(String msg) {
count++;
Log.d("console", msg);
}

@JavascriptInterface
public void info(String msg) {
count++;
Log.i("console", msg);
}

@JavascriptInterface
public void error(String msg) {
count++;
Log.e("console", msg);
}

@JavascriptInterface
public int count() {
return count;
}
}

@Override
Expand Down
41 changes: 41 additions & 0 deletions quickjs-android/src/androidTest/java/com/quickjs/JSObjectTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.quickjs;

import android.util.Log;
import android.webkit.JavascriptInterface;

import com.quickjs.JSArray;
import com.quickjs.JSContext;
Expand Down Expand Up @@ -247,4 +248,44 @@ public void test2() {
array.set("www", value);
array.set("www", JSValue.NULL());
}

@Test
public void executeVoidFunction() {

}

@Test
public void addJavascriptInterface() {
context.addJavascriptInterface(new Console(), "console");
context.executeVoidScript("console.log('Hello World')", null);
int count = context.executeIntegerScript("console.count()", null);
assertEquals(1, count);
}

public static class Console {
int count = 0;

@JavascriptInterface
public void log(String msg) {
count++;
Log.d("console", msg);
}

@JavascriptInterface
public void info(String msg) {
count++;
Log.i("console", msg);
}

@JavascriptInterface
public void error(String msg) {
count++;
Log.e("console", msg);
}

@JavascriptInterface
public int count() {
return count;
}
}
}
61 changes: 61 additions & 0 deletions quickjs-android/src/main/java/com/quickjs/JSObject.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.quickjs;

import android.webkit.JavascriptInterface;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class JSObject extends JSValue {

public JSObject(JSContext context) {
Expand Down Expand Up @@ -141,6 +147,61 @@ protected Object executeFunction(TYPE expectedType, String name, JSArray paramet
return JSValue.checkType(object, expectedType);
}

public void addJavascriptInterface(Object obj, String interfaceName) {
Method[] methods = obj.getClass().getMethods();
JSObject object = new JSObject(context);
for (Method method : methods) {
if (method.getAnnotation(JavascriptInterface.class) == null) {
continue;
}
String functionName = method.getName();
if (method.getReturnType().equals(Void.TYPE)) {
object.registerJavaMethod((receiver, args) -> {
try {
method.invoke(obj, getParameters(method, args));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}, functionName);
} else {
object.registerJavaMethod((receiver, args) -> {
try {
return method.invoke(obj, getParameters(method, args));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}, functionName);
}
}
set(interfaceName, object);
}

private Object[] getParameters(Method method, JSArray args) {
Object[] objects = new Object[args.length()];
Type[] types = method.getGenericParameterTypes();
for (int i = 0; i < objects.length; i++) {
Type type = types[i];
if (type == int.class || type == Integer.class) {
objects[i] = args.getInteger(i);
} else if (type == double.class || type == Double.class) {
objects[i] = args.getDouble(i);
} else if (type == boolean.class || type == Boolean.class) {
objects[i] = args.getBoolean(i);
} else if (type == String.class) {
objects[i] = args.getString(i);
} else if (type == JSArray.class) {
objects[i] = args.getArray(i);
} else if (type == JSObject.class || type == JSFunction.class) {
objects[i] = args.getObject(i);
} else {
throw new RuntimeException("Type error");
}
}
return objects;
}

static class Undefined extends JSObject {

Undefined(JSContext context, long tag, int u_int32, double u_float64, long u_ptr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.quickjs;

public interface JavaCallback {
Object invoke(JSObject receiver,JSArray array);
Object invoke(JSObject receiver,JSArray args);
}
1 change: 1 addition & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./gradlew uploadArchives

0 comments on commit 9bb1a44

Please sign in to comment.