pull/585/head
FongMi 1 year ago
parent fb04e3b95f
commit b642407fa2
  1. 10
      quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java
  2. 47
      quickjs/src/main/java/com/fongmi/quickjs/method/Function.java
  3. 3
      quickjs/src/main/java/com/fongmi/quickjs/method/Global.java
  4. 2
      quickjs/src/main/java/com/fongmi/quickjs/utils/Async.java

@ -3,10 +3,10 @@ package com.fongmi.quickjs.crawler;
import android.content.Context;
import com.fongmi.quickjs.bean.Res;
import com.fongmi.quickjs.method.Async;
import com.fongmi.quickjs.method.Console;
import com.fongmi.quickjs.method.Global;
import com.fongmi.quickjs.method.Local;
import com.fongmi.quickjs.utils.Async;
import com.fongmi.quickjs.utils.JSUtil;
import com.fongmi.quickjs.utils.Module;
import com.github.catvod.utils.Asset;
@ -59,7 +59,6 @@ public class Spider extends com.github.catvod.crawler.Spider {
}
private Object call(String func, Object... args) throws Exception {
//return executor.submit((Function.call(jsObject, func, args))).get();
return CompletableFuture.supplyAsync(() -> Async.run(jsObject, func, args), executor).join().get();
}
@ -149,7 +148,7 @@ public class Spider extends com.github.catvod.crawler.Spider {
private void initializeJS() throws Exception {
submit(() -> {
createCtx();
createDex();
createFun();
createObj();
return null;
}).get();
@ -159,7 +158,6 @@ public class Spider extends com.github.catvod.crawler.Spider {
ctx = QuickJSContext.create();
ctx.setConsole(new Console());
ctx.evaluate(Asset.read("js/lib/http.js"));
Global.create(ctx, executor).setProperty();
ctx.getGlobalObject().setProperty("local", Local.class);
ctx.setModuleLoader(new QuickJSContext.BytecodeModuleLoader() {
@Override
@ -174,9 +172,9 @@ public class Spider extends com.github.catvod.crawler.Spider {
});
}
private void createDex() {
private void createFun() {
try {
if (dex == null) return;
Global.create(ctx, executor);
Class<?> clz = dex.loadClass("com.github.catvod.js.Function");
clz.getDeclaredConstructor(QuickJSContext.class).newInstance(ctx);
} catch (Throwable e) {

@ -1,47 +0,0 @@
package com.fongmi.quickjs.method;
import com.whl.quickjs.wrapper.JSCallFunction;
import com.whl.quickjs.wrapper.JSFunction;
import com.whl.quickjs.wrapper.JSObject;
import java.util.concurrent.Callable;
public class Function implements Callable<Object> {
private final JSObject object;
private final Object[] args;
private final String name;
private Object result;
public static Function call(JSObject object, String name, Object[] args) {
return new Function(object, name, args);
}
private Function(JSObject object, String name, Object[] args) {
this.object = object;
this.name = name;
this.args = args;
}
@Override
public Object call() throws Exception {
JSFunction function = object.getJSFunction(name);
if (function == null) return null;
result = function.call(args);
if (result instanceof JSObject) then(result);
return result;
}
private void then(Object result) {
JSObject promise = (JSObject) result;
JSFunction then = promise.getJSFunction("then");
if (then != null) then.call(callback);
}
private final JSCallFunction callback = new JSCallFunction() {
@Override
public Object call(Object... args) {
return result = args[0];
}
};
}

@ -40,9 +40,10 @@ public class Global {
this.executor = executor;
this.timer = new Timer();
this.ctx = ctx;
setProperty();
}
public void setProperty() {
private void setProperty() {
for (Method method : getClass().getMethods()) {
if (!method.isAnnotationPresent(JSMethod.class)) continue;
ctx.getGlobalObject().setProperty(method.getName(), args -> {

@ -1,4 +1,4 @@
package com.fongmi.quickjs.method;
package com.fongmi.quickjs.utils;
import com.whl.quickjs.wrapper.JSCallFunction;
import com.whl.quickjs.wrapper.JSFunction;
Loading…
Cancel
Save