{"id":15578189,"url":"https://github.com/taoweiji/quickjs-android","last_synced_at":"2025-08-20T04:32:55.813Z","repository":{"id":47161923,"uuid":"370052095","full_name":"taoweiji/quickjs-android","owner":"taoweiji","description":"Android Bindings for QuickJS, A fine little javascript engine.","archived":false,"fork":false,"pushed_at":"2021-10-31T14:26:47.000Z","size":1036,"stargazers_count":192,"open_issues_count":14,"forks_count":41,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-12-09T23:04:15.902Z","etag":null,"topics":["android","engine","javascript","quickjs"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taoweiji.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-23T12:51:22.000Z","updated_at":"2024-11-18T13:33:31.000Z","dependencies_parsed_at":"2022-08-12T13:11:53.685Z","dependency_job_id":null,"html_url":"https://github.com/taoweiji/quickjs-android","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taoweiji%2Fquickjs-android","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taoweiji%2Fquickjs-android/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taoweiji%2Fquickjs-android/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taoweiji%2Fquickjs-android/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taoweiji","download_url":"https://codeload.github.com/taoweiji/quickjs-android/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230394228,"owners_count":18218707,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","engine","javascript","quickjs"],"created_at":"2024-10-02T19:07:19.680Z","updated_at":"2024-12-19T07:07:04.394Z","avatar_url":"https://github.com/taoweiji.png","language":"C","readme":"# quickjs-android\n[![Download](https://maven-badges.herokuapp.com/maven-central/io.github.taoweiji.quickjs/quickjs-android/badge.svg)](https://search.maven.org/search?q=io.github.taoweiji.quickjs)\n\n\n[quickjs-android](https://github.com/taoweiji/quickjs-android) 是 [QuickJS](https://github.com/bellard/quickjs) JavaScript 引擎的 Android 接口框架，整体基于面向对象设计，提供了自动GC功能，使用简单。armeabi-v7a 的大小仅 350KB，是 [Google V8](https://github.com/v8/v8) 不错的替代品，启动速度比 V8 快，内存占用更低，支持 [ES2020](https://tc39.es/ecma262/)。\n\n- armeabi-v7a 平台下，整体占用apk空间仅 350KB；\n- JS对象自动GC，无需手动释放；\n- 支持 ES6 Module，可以使用 import、export 函数；\n- 支持 Node.js 的 CommonJS 规范，可以使用 require、exports 函数；\n- 支持绑定 Java 注解函数；\n- 支持通过 Java Function Callback 函数注册JS函数；\n- 内置 Event Queue，开发者可以在任意线程执行代码，无需关心JS单线程问题；\n\n### 使用教程\n\n##### 引入依赖\n\n```groovy\nimplementation 'io.github.taoweiji.quickjs:quickjs-android:1.+'\n```\n\n##### 简单示例\n\n```java\nQuickJS quickJS = QuickJS.createRuntime();\nJSContext context = quickJS.createContext();\nint result = context.executeIntegerScript(\"var a = 2+10;\\n a;\", \"file.js\");\ncontext.close();\nquickJS.close();\n```\n\n\n\n### 对象介绍\n\n##### QuickJS\n\n运行环境，可以创建多个运行时环境，不同的环境之间不能共享对象，不使用的时候需要销毁。\n\n```java\nQuickJS quickJS = QuickJS.createRuntime();\n// 如果需要在多线程执行，必须创建带有线程池的环境\n// QuickJS quickJS = QuickJS.createRuntimeWithEventQueue();\n```\n\n##### JSContext\n\n由 QuickJS 创建，一个 QuickJS 可以创建多个 JSContext，不使用的时候需要销毁。\n\n```java\nJSContext context = quickJS.createContext();\nint result = context.executeIntegerScript(\"var a = 2+10;\\n a;\", \"file.js\");\nString result = context.executeStringScript(\"'Hello World';\", \"file.js\");\ncontext.close();\n```\n\n##### JSObject\n\n```java\nJSObject user = new JSObject(context).set(\"name\", \"Wiki\").set(\"age\", 18).set(\"time\",System.currentTimeMillis());\nLog.e(\"QuickJS\", String.valueOf(user.getString(\"name\")));\nLog.e(\"QuickJS\", String.valueOf(user.getInteger(\"age\")));\nLog.e(\"QuickJS\", String.valueOf(user.getDouble(\"time\")));\n\nuser.registerJavaMethod(new JavaVoidCallback() {\n    @Override\n    public void invoke(JSObject receiver, JSArray args) {\n        Log.e(\"QuickJS\", args.getString(0));\n    }\n}, \"log\");\nuser.executeVoidFunction(\"log\", new JSArray(context).push(\"Hello World\"));\n```\n##### JSArray\n\n```java\nJSArray array = new JSArray(context).push(1).push(3.14).push(true).push(\"Hello World\");\nLog.e(\"QuickJS\", String.valueOf(array.getInteger(0)));\nLog.e(\"QuickJS\", String.valueOf(array.getDouble(1)));\n```\n\n##### JSFunction\n\n```java\nJSFunction log = new JSFunction(context, new JavaVoidCallback() {\n    @Override\n    public void invoke(JSObject receiver, JSArray args) {\n        Log.e(\"QuickJS\", args.getString(0));\n    }\n});\nJSFunction message = new JSFunction(context, new JavaCallback() {\n    @Override\n    public Object invoke(JSObject receiver, JSArray array) {\n        return \"Hello World\";\n    }\n});\ncontext.set(\"console\", new JSObject(context).set(\"log\", log).set(\"message\", message));\ncontext.executeVoidScript(\"console.log(console.message())\", null);\n```\n\n##### addJavascriptInterface\n\n```java\npublic class Console {\n    int count = 0;\n\n    @JavascriptInterface\n    public void log(String msg) {\n        count++;\n        Log.d(\"console\", msg);\n    }\n\n    @JavascriptInterface\n    public void info(String msg) {\n        count++;\n        Log.i(\"console\", msg);\n    }\n\n    @JavascriptInterface\n    public void error(String msg) {\n        count++;\n        Log.e(\"console\", msg);\n    }\n\n    @JavascriptInterface\n    public int count() {\n        return count;\n    }\n}\n\ncontext.addJavascriptInterface(new Console(), \"console\");\ncontext.executeVoidScript(\"console.log('Hello World')\", null);\nint count = context.executeIntegerScript(\"console.count()\", null);\nLog.d(\"console\", String.valueOf(count));\n```\n\n#### QuickJS\n\n| 方法                             | 说明       |\n| -------------------------------- | ---------- |\n| static QuickJS createRuntime() | 创建运行时 |\n| JSContext createContext()        | 创建上下文 |\n| void close()                     | 销毁引擎 |\n\n#### JSValue\n\n对象会自动回收，开发者无需手动close()\n\n| 方法                                         | 说明              |\n| -------------------------------------------- | ----------------- |\n| static JSObject Undefined(JSContext context) | 获取Undefined对象 |\n| static JSValue NULL()                        | 获取NULL对象      |\n| TYPE getType()                               | 获取数据类型      |\n| boolean isUndefined()                        |                   |\n\n\n#### JSObject\n\n继承JSValue\n\n| 方法                           | 说明              |\n| ------------------------------ | ----------------- |\n| set(key, value)                | 设置属性，支持int、boolean、double、String、JSValue |\n| int getInteger(String key)     | 返回值int对象值，如果没有就会返回0 |\n| boolean getBoolean(String key) | 返回值boolean对象值，如果没有就会返回false |\n| double getDouble(String key)   | 返回值double对象值，如果没有就会返回0 |\n| String getString(String key)   | 返回值String对象值，如果没有就会返回null |\n| JSArray getArray(String key)   | 返回值JSArray对象值，如果没有就会返回null |\n| JSObject getObject(String key) | 可能会返回JSObject、JSArray、JSFunction，如果没有就会返回null |\n| registerJavaMethod(JavaCallback callback, String jsFunctionName) | 注册JS函数，调用函数会执行java的Callback，带有返回值 |\n| registerJavaMethod(JavaVoidCallback callback, String jsFunctionName) | 注册JS函数，调用函数会执行java的Callback，不带返回值 |\n| Object executeFunction(String name, JSArray parameters) | 可能会返回Integer、Double、Boolean、String、JSArray、JSObject、JSFunction、null |\n| double executeDoubleFunction(String name, JSArray parameters) | 返回 double，默认返回 0                     |\n| boolean executeBooleanFunction(String name, JSArray parameters) | 返回boolean，默认人会false                |\n| String executeStringFunction(String name, JSArray parameters) | 返回String，默认返回null |\n| JSArray executeArrayFunction(String name, JSArray parameters) | 返回JSArray，默认返回null |\n| JSObject executeObjectFunction(String name, JSArray parameters) | 可能会返回JSObject、JSArray、JSFunction，默认返回null |\n| void executeVoidFunction(String name, JSArray parameters) | 没有返回值 |\n| Object executeFunction2(String name, Object... parameters) | 可能返回Integer、Double、Boolean、String、JSArray、JSObject、JSFunction、null，入参为java数组，仅支持Integer、Double、Boolean、String、JSArray、JSObject、JSFunction、null |\n| boolean contains(String key) | 是否包含该字段 |\n| String[] getKeys() | 获取属性列表 |\n\n\n#### JSArray\n\n继承JSObject\n\n| 方法                                                         | 说明              |\n| ------------------------------------------------------------ | ----------------- |\n| push(value)                                                  | 设置属性，支持int、boolean、double、String、JSValue |\n| int getInteger(String key) | 返回值int对象值，如果没有就会返回0 |\n| boolean getBoolean(String key) | 返回值boolean对象值，如果没有就会返回false |\n| double getDouble(String key) | 返回值double对象值，如果没有就会返回0 |\n| String getString(String key) | 返回值String对象值，如果没有就会返回null |\n| JSArray getArray(String key) | 返回值JSArray对象值，如果没有就会返回null |\n| JSObject getObject(String key) | 可能会返回JSObject、JSArray、JSFunction，如果没有就会返回null |\n| length()                                                     | 数组大小  |\n\n#### JSFunction\n\n继承JSObject\n\n| 方法                                                         | 说明     |\n| ------------------------------------------------------------ | -------- |\n| JSFunction(JSContext context, JavaCallback callback)         | 构造函数 |\n| JSFunction(JSContext context, JavaVoidCallback callback)     | 构造函数 |\n| Object call(JSValue.TYPE type, JSObject receiver, JSArray parameters) | 调用方法 |\n\n#### JSContext\n\n继承JSObject，拥有JSObject全部方法，对象本身是全局对象\n\n| 方法                                                         | 说明                                                         |\n| ------------------------------------------------------------ | ------------------------------------------------------------ |\n| void close()                                                 | 销毁上下文                                                   |\n| int executeIntegerScript(String source, String fileName)     | 执行js脚本                                                   |\n| double executeDoubleScript(String source, String fileName)   | 执行js脚本                                                   |\n| String executeStringScript(String source, String fileName)   | 执行js脚本                                                   |\n| boolean executeBooleanScript(String source, String fileName) | 执行js脚本                                                   |\n| Object executeScript(String source, String fileName)         | 执行js脚本，可能返回Integer、Double、Boolean、String、JSArray、JSObject、JSFunction、null |\n| void executeVoidScript(String source, String fileName)       | 执行js脚本，无返回值                                         |\n| JSArray executeArrayScript(String source, String fileName)   | 执行js脚本，返回值为JSArray                                  |\n| JSObject executeObjectScript(String source, String fileName) | 执行js脚本，可能会返回JSObject、JSArray、JSFunction          |\n\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaoweiji%2Fquickjs-android","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaoweiji%2Fquickjs-android","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaoweiji%2Fquickjs-android/lists"}