- 🔭 I’m currently working on Low-Code Platform.
- 👯 I'm looking for interested friends to join us.
harlonwang / quickjs-wrapper Goto Github PK
View Code? Open in Web Editor NEWQuickJS wrapper for Android/JVM
License: Apache License 2.0
QuickJS wrapper for Android/JVM
License: Apache License 2.0
Hello! I've seen that you have applied some bugfixes and improvements to QuickJS, which seems to be abandoned. Would you please consider forking the original repository yourself and applying your changes on top? I would be happy to use your fork and possibly contribute with more improvements.
Thanks!
0.0.20aar包中有,是不是代码没提交
同时创建多个 QuickJSContext
QuickJSContext context1 = QuickJSContext.create();
QuickJSContext context2 = QuickJSContext.create();
destroy 其中的一个
context1.destroy();
context2 也无法使用了
Todo
实际项目中用了类似的逻辑,Promise内部执行JS,这段JS会调用到Java,然后在回调里执行reject(res); so库会崩溃。
下面是模拟实际场景写出的demo,也会出现崩溃,但是错误信息不一致。
//附测试代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QuickJSLoader.init();
testPromiseCrash();
}
//运行app执行,会崩溃
public void testPromiseCrash() {
QuickJSContext jsContext = QuickJSContext.create();
JSObject pofeng = jsContext.createNewJSObject();
JSObject gol = jsContext.getGlobalObject();
gol.setProperty("pofeng", pofeng);
pofeng.setProperty("getSystemInfo", new JSCallFunction() {
@Override
public Object call(Object... args) {
((JSFunction) ((JSObject) args[0]).getJSObject("success")).call("我来自Java的值");
return "我来自Java的值";
}
});
String js = "new Promise((resolve, reject) => {\n" +
" pofeng.getSystemInfo({\n" +
" success: res => {\n" +
" reject(res);\n" +
" }\n" +
" })\n" +
" })";
try {
jsContext.evaluate(js);
} catch (Exception e) {
e.printStackTrace();
}
}
https://github.com/openwebf/quickjs 性能优化提升40+ 如果可以建议更换这个
Promise错误能否支持一下?
@test
fun testPromise(){
context.evaluate("""
new Promise(
()=>console.log(aaaa)
)
""".trimIndent())
}
您好,我執行以下代發發現沒有出現錯誤訊息
console.log("666")
console.log(gg())
console.log("路飞开始调试了!!!");
第 2 行應該要顯示 'gg' is not defined
再麻煩了,謝謝。
No implementation found for long com.whl.quickjs.wrapper.QuickJSContext.createContext() (tried Java_com_whl_quickjs_wrapper_QuickJSContext_createContext and Java_com_whl_quickjs_wrapper_QuickJSContext_createContext__)
我该怎样在其他线程调用JSCallFunction.call
Reference code: in qjs.c
static int eval_file(JSContext *ctx, const char *filename, int module)
{
uint8_t *buf;
int ret, eval_flags;
size_t buf_len;
buf = js_load_file(ctx, &buf_len, filename);
if (!buf) {
perror(filename);
exit(1);
}
if (module < 0) {
module = (has_suffix(filename, ".mjs") ||
JS_DetectModule((const char *)buf, buf_len));
}
if (module)
eval_flags = JS_EVAL_TYPE_MODULE;
else
eval_flags = JS_EVAL_TYPE_GLOBAL;
ret = eval_buf(ctx, buf, buf_len, filename, eval_flags);
js_free(ctx, buf);
return ret;
}
QuickJS 每个方法前增加检测,伪代码如下:
public void call() {
if(notSameThread()) {
throw new Exception("Invoke this must be in same thread.");
}
}
复现代码:
@Test
public void testReturnFree() {
QuickJSContext context = QuickJSContext.create();
context.getGlobalObject().setProperty("test", (JSCallFunction) args -> {
JSObject result = context.parseJSON("{}");
return result;
});
context.evaluate("test();test();test();");
}
Todo: c 层异常后需要抛回到 Java 层,不然外部依赖的是 so 库,无法识别实际错误信息
测试代码如下:
public void testNullExceptionWithProperty() {
QuickJSContext context = QuickJSContext.create();
JSObject object = context.createNewJSObject();
object.getJSObject(null);
context.destroyContext();
}
Will be support.
請問更新至 0.19.1 有需要調整什麼嗎?
因為我一樣的程式在 0.19.1 無法運作,沒看到錯誤訊息。
JVM 下可以编译不同平台的动态链接库,并上传到 Maven 仓库中
請問 minSdk 可以降到 17 嗎
如果不行希望可以在 System.loadLibrary 加個 try catch Throwable
能否支持JSRuntime运行时 创建多个JSContext上下文?
如果只有一个JSContext的话加载多个脚本存在同名属性方法,会导致全局GlobalObjec冲突。
我看quickjs文档是支持一个JSRuntime多个JSContext,每个JSContext都有自己的全局对象和系统对象。
向大佬确认一下。。。
看这个库的方法感觉好像一个App里面只会一个JSRuntime
看了一下另外的两个类似的库
https://github.com/taoweiji/quickjs-android
https://github.com/seven332/quickjs-android
都是支持创建多个JSRunTime的(类似JSRuntime runtime = quickJS.createJSRuntime()之类的方法)
some module name start with "http"
有没有办法直接创建一个 Uint8Array 或 ArrayBuffer 的 JS 值?这个ArrayBuffer可以通过java来写入和读取?
目前只有JSArray ,只能
JSArray array = context.createNewJSArray();
for (int i = 0; i < bytes.length; i++) array.set((int) bytes[i], i);
这样大数据会非常慢。
Todo fix
复现方式:TinyUIListView 连续滚动10分钟,商米V2,P2设备
請問怎麼實現 js 的 setTimeout func
我有看到 https://github.com/HarlonWang/quickjs-wrapper/blob/main/remarks.md 裡面有範例
但是 context.call(argFunc, context.getGlobalObject()); 現在是私有的,無法呼叫。
module_a.js
export function name(){ }
module_b.js
import name from "./module_a.js"
export function name(){ return name() }
然后用 qjsc 编译得到 a 和 b 的 ByteCode。
加载 模块
context.setModuleLoader(new ModuleLoader() {
@Override
public boolean isBytecodeMode() {
return true;
}
@Override
public String getModuleStringCode(String moduleName) {
return Es6ModuleLoader.get().getModuleStringCode(moduleName);
}
@Override
public byte[] getModuleBytecode(String moduleName) {
//这里 moduleName 都是相对路径
//module_a.js 是 http://……/module_a.js 正确
//module_b.js 是 ./module_b.js 这里就变成了相对路径了、由于没有 base_module_name 所以没有办法合并路径了
}
});
context.evaluateModule("import * as all from 'http://……/module_a.js'")
Todo
复现代码如下:
@Test
public void testPromiseUnHandledExceptionWithCall() {
QuickJSContext context = QuickJSContext.create();
context.getGlobalObject().setProperty("nativeCall", new JSCallFunction() {
@Override
public Object call(Object... args) {
JSFunction function = (JSFunction) args[0];
function.call();
return null;
}
});
context.evaluate("const getData = async () => {\n" +
"\tconst res = await new Promise(() => {\n" +
"\t\twindow.setTimeout();\n" +
"\t});\n" +
"}\n" +
"\n" +
"// function nativeCall(fn) {\n" +
"// \tfn.call();\n" +
"// }\n" +
"\n" +
"// console.log(getData);\n" +
"\n" +
"nativeCall(getData);");
context.destroyContext();
}
Like this :
In Java
class TestJava {
public void log(String message) {
System.out.println(message);
}
}
@Test
public void testSetJavaClass() {
QuickJSContext context = QuickJSContext.create();
context.setProperty(context.getGlobalObject(), "test", TestJava.class);
}
In JavaScript
test.log("123");
// print 123
我该怎样在其他线程调用JSCallFunction.call
即支持动态设置quickjs.so的路径
增加 ES6 模块加载能力,支持 import 和 export 命令。
javascript 函数
async function getData(){
const data = await new Promise((resolve, reject) => {
const complete = (ok, data) => ok ? resolve(data) : reject(data);
http(url, complete)
})
return data;
}
java方法
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
handler().post(() -> complete.call(false, "error"));
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {
handler().post(() -> complete.call(true, "ok"));
}
});
JSFunction func = jsObject.getJSFunction("getData");
Object data = func.call();
//什么获取 js 函数 getData 的返回值?
使QuickJSContext实现 AutoCloseable,现在销毁 JSContext 需要执行两个方法,太麻烦了。
Todo
Todo
Test:
new Date(new Date().toLocaleDateString()).getTime();
// output NAN in QuickJS
参考浏览器或者 node
Todo
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.