标签:
J2V8是基于webkit中内核js v8引擎的java 开源项目,实现了java与javaScript的互相调用,弥补了2种语言各自的缺陷
下载地址
http://mvnrepository.com/search?q=com.eclipsesource.j2v8
http://eclipsesource.com/blogs/2015/02/25/announcing-j2v8-2-0/
1.可实现方法动态生成,注册和调用,可以解决java 方法爆棚问题
2.降低了交互难度,避免了使用webView
3.可直接调用大多数javascript api,让类似解析JSON变得更容易,方便
4.j2v8提供了多种数据结构,并实现了新的缓存数据存储方式
5.实现了动态脚本执行,实现了多线程
6.Javascript更容易调用Android代码层,开发语言使用Javascript+原生UI更容易开发Native App
由于使用了v8引擎,apk会变得更大
不支持Java类方法注册,不太支持Javascript的原型连中的call,apply
释放过程过于繁琐,内存不是ARC自动释放
不支持按脚本路径加载脚本,需要通过IO流转为字符串才能将脚本加入
好了,以上是J2V8的相关信息,下面欣赏一下这个开源项目的魅力
package org.twt.zipjar.test; import java.util.ArrayList; import java.util.List; import com.eclipsesource.v8.V8; import com.eclipsesource.v8.V8Array; import com.eclipsesource.v8.V8Object; import com.eclipsesource.v8.utils.V8Map; import com.eclipsesource.v8.utils.V8ObjectUtils; import com.eclipsesource.v8.utils.V8Runnable; import com.eclipsesource.v8.utils.V8Thread; public class J2V8TestCase { public static void main(String[] args) { } public void testMultiV8Threads() throws InterruptedException { final V8 v8 = V8.createV8Runtime(); final List<Thread> threads = new ArrayList<Thread>(); final List<Object> mergeSortResults = new ArrayList<Object>(); for (int i = 0; i < 10; i++) { V8Thread t = new V8Thread(new V8Runnable() { @Override public void run(V8 v8) { synchronized (threads) { V8Array data = new V8Array(v8); int max = 100; for (int i = 0; i < max; i++) { data.push(max - i); } V8Array parameters = new V8Array(v8).push(data); mergeSortResults.add(V8ObjectUtils.toList(parameters)); parameters.release(); data.release(); } } }); threads.add(t); } for (Thread thread : threads) { thread.start(); } for (Thread thread : threads) { thread.join(); } } private static void declareFunctions() { V8 v8 = V8.createV8Runtime(); v8.executeVoidScript("function getPerson(first, last, age) {return {‘first‘:first, ‘last‘:last, ‘age‘:age};}"); V8Array parameters = new V8Array(v8); parameters.push("John"); parameters.push("Smith"); parameters.push(7); V8Object result = v8.executeObjectFunction("getPerson", parameters); parameters.release(); result.release(); } private static void TestMap() { V8 runtime = V8.createV8Runtime(); V8Map<String> v8Map = new V8Map<String>(); V8Object v8Object = new V8Object(runtime); v8Map.put(v8Object, "foo"); v8Object.release(); runtime.registerResource(v8Map); runtime.release(true); } private static void jsFunctionCall() { V8 v8 = V8.createV8Runtime(); V8Array param = new V8Array(v8); V8Array arrAList = new V8Array(v8).push(0).push("0"); V8Array arrBList = new V8Array(v8).push(1).push("1"); param.push(arrAList).push(arrBList); V8Array result = v8.executeArrayScript("var s = [];Array.prototype.push.apply(s,[1,2,3]);s"); System.out.println(V8ObjectUtils.toList(result)); arrAList.release(); arrBList.release(); param.release(); result.release(); v8.release(); } private static void jsonObjectCall() { V8 v8 = V8.createV8Runtime(); V8Object v8Object = new V8Object(v8).add("foo", "bar") add("name", "zhangsan") .add("age", 20); V8Object json = v8.getObject("JSON"); V8Array params = new V8Array(v8).push(v8Object); String result = (String) json.executeFunction("stringify", params); V8Array params2 = new V8Array(v8).push(result); V8Object result2 = json.executeObjectFunction("parse", params2); System.out.println(result); System.out.println(result2.getString("name")); params.release(); params2.release(); result2.release(); json.release(); v8Object.release(); v8.release(); } private static void dataCache() { V8 v8 = V8.createV8Runtime(); v8.add("name", "zhangsan"); System.out.println(v8.get("name")); v8.release(); } private static void callJavaNativeMethod() { V8 v8 = V8.createV8Runtime(); v8.registerJavaMethod(new J2V8TestCase(), "print", "print",new Class[]{String.class}); String s = "Hello World"; v8.executeVoidScript("print(‘"+s+"‘)"); v8.release(); } private static void executeArrayScript() { V8 v8 = V8.createV8Runtime(); V8Array result = v8.executeArrayScript("var buffer = new ArrayBuffer(256);\n" + "var i32 = new Int32Array(buffer);\n" + "i32[0] = 1;\n" + "i32[1] = 3;\n" + "i32[2] = i32[0] + i32[1];\n" + "i32;"); int[] ints = result.getIntegers(0, 3); result.release(); v8.release(); } public void print(String s){ System.out.println(s); } }
标签:
原文地址:http://my.oschina.net/ososchina/blog/504709