android: enable setTimeout and setInterval

This commit is contained in:
pengfei.zhou 2021-11-05 15:40:25 +08:00 committed by osborn
parent bb17b74b99
commit 5c74729fbc
6 changed files with 34 additions and 25 deletions

View File

@ -83,6 +83,9 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
initJSEngine(); initJSEngine();
injectGlobal(); injectGlobal();
initDoricRuntime(); initDoricRuntime();
if (mDoricJSE instanceof DoricWebViewJSExecutor) {
mDoricJSE.loadJS("_prepared();", "");
}
initialized = true; initialized = true;
globalProfile.end(DoricPerformanceProfile.PART_INIT); globalProfile.end(DoricPerformanceProfile.PART_INIT);
} }

View File

@ -43,7 +43,7 @@ import pub.doric.utils.DoricLog;
/** /**
* @Description: This contains a webView which is used for executing JavaScript * @Description: This uses a webView to execute JavaScript
* @Author: pengfei.zhou * @Author: pengfei.zhou
* @CreateDate: 2021/11/3 * @CreateDate: 2021/11/3
*/ */
@ -215,12 +215,10 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
@Override @Override
public JSDecoder invokeMethod(String objectName, String functionName, JavaValue[] javaValues, boolean hashKey) throws JSRuntimeException { public JSDecoder invokeMethod(String objectName, String functionName, JavaValue[] javaValues, boolean hashKey) throws JSRuntimeException {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
for (int i = 0; i < javaValues.length; i++) { for (JavaValue javaValue : javaValues) {
JavaValue javaValue = javaValues[i];
JSONObject jsonObject = wrapJavaValue(javaValue); JSONObject jsonObject = wrapJavaValue(javaValue);
jsonArray.put(jsonObject); jsonArray.put(jsonObject);
} }
returnFuture = new SettableFuture<>(); returnFuture = new SettableFuture<>();
String script = String.format( String script = String.format(
"__invokeMethod('%s','%s','%s')", "__invokeMethod('%s','%s','%s')",

View File

@ -85,6 +85,9 @@ function _rawValue(v) {
return v.value; return v.value;
case "object": case "object":
case "array": case "array":
if (typeof v.value === 'string') {
return JSON.parse(v.value);
}
return v.value; return v.value;
default: default:
return undefined; return undefined;
@ -104,21 +107,13 @@ function __injectGlobalFunction(name) {
}); });
} }
function __invokeMethod(objectName, functionName, stringifiedArgs) { function __invokeMethod(objectName, functionName, stringifiedArgs) {
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`);
try { try {
const thisObject = Reflect.get(window, objectName); const thisObject = Reflect.get(window, objectName);
const thisFunction = Reflect.get(thisObject, functionName); const thisFunction = Reflect.get(thisObject, functionName);
const args = JSON.parse(stringifiedArgs); const args = JSON.parse(stringifiedArgs);
args.forEach(e => {
NativeClient.log(`Arg:${e},${typeof e}`);
});
const rawArgs = args.map(e => _rawValue(e)); const rawArgs = args.map(e => _rawValue(e));
rawArgs.forEach(e => {
NativeClient.log(`RawArg:${e},${typeof e}`);
});
const ret = Reflect.apply(thisFunction, thisObject, rawArgs); const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
const returnVal = JSON.stringify(_wrappedValue(ret)); const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : "";
NativeClient.log(`return:${returnVal}`);
NativeClient.returnNative(returnVal); NativeClient.returnNative(returnVal);
} }
catch (e) { catch (e) {
@ -126,3 +121,9 @@ function __invokeMethod(objectName, functionName, stringifiedArgs) {
NativeClient.returnNative(""); NativeClient.returnNative("");
} }
} }
function _prepared() {
window.setTimeout = Reflect.get(window, "doricSetTimeout");
window.setInterval = Reflect.get(window, "doricSetInterval");
window.clearTimeout = Reflect.get(window, "doricClearTimeout");
window.clearInterval = Reflect.get(window, "doricClearInterval");
}

View File

@ -119,18 +119,23 @@ function __injectGlobalFunction(name: string) {
} }
function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string) { function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string) {
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`)
try { try {
const thisObject = Reflect.get(window, objectName); const thisObject = Reflect.get(window, objectName);
const thisFunction = Reflect.get(thisObject, functionName); const thisFunction = Reflect.get(thisObject, functionName);
const args = JSON.parse(stringifiedArgs) as WrappedValue[]; const args = JSON.parse(stringifiedArgs) as WrappedValue[];
const rawArgs = args.map(e => _rawValue(e)); const rawArgs = args.map(e => _rawValue(e));
const ret = Reflect.apply(thisFunction, thisObject, rawArgs); const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
const returnVal = JSON.stringify(_wrappedValue(ret)) const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : ""
NativeClient.log(`return:${returnVal}`)
NativeClient.returnNative(returnVal) NativeClient.returnNative(returnVal)
} catch (e) { } catch (e) {
NativeClient.log(`error:${e},${(e as any).stack}`) NativeClient.log(`error:${e},${(e as any).stack}`)
NativeClient.returnNative("") NativeClient.returnNative("")
} }
} }
function _prepared() {
window.setTimeout = Reflect.get(window, "doricSetTimeout");
window.setInterval = Reflect.get(window, "doricSetInterval");
window.clearTimeout = Reflect.get(window, "doricClearTimeout");
window.clearInterval = Reflect.get(window, "doricClearInterval");
}

View File

@ -26,3 +26,4 @@ declare function _rawValue(v: WrappedValue): RawValue;
declare function _injectGlobalObject(name: string, args: string): void; declare function _injectGlobalObject(name: string, args: string): void;
declare function __injectGlobalFunction(name: string): void; declare function __injectGlobalFunction(name: string): void;
declare function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string): void; declare function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string): void;
declare function _prepared(): void;

View File

@ -83,6 +83,9 @@ function _rawValue(v) {
return v.value; return v.value;
case "object": case "object":
case "array": case "array":
if (typeof v.value === 'string') {
return JSON.parse(v.value);
}
return v.value; return v.value;
default: default:
return undefined; return undefined;
@ -102,21 +105,13 @@ function __injectGlobalFunction(name) {
}); });
} }
function __invokeMethod(objectName, functionName, stringifiedArgs) { function __invokeMethod(objectName, functionName, stringifiedArgs) {
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`);
try { try {
const thisObject = Reflect.get(window, objectName); const thisObject = Reflect.get(window, objectName);
const thisFunction = Reflect.get(thisObject, functionName); const thisFunction = Reflect.get(thisObject, functionName);
const args = JSON.parse(stringifiedArgs); const args = JSON.parse(stringifiedArgs);
args.forEach(e => {
NativeClient.log(`Arg:${e},${typeof e}`);
});
const rawArgs = args.map(e => _rawValue(e)); const rawArgs = args.map(e => _rawValue(e));
rawArgs.forEach(e => {
NativeClient.log(`RawArg:${e},${typeof e}`);
});
const ret = Reflect.apply(thisFunction, thisObject, rawArgs); const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
const returnVal = JSON.stringify(_wrappedValue(ret)); const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : "";
NativeClient.log(`return:${returnVal}`);
NativeClient.returnNative(returnVal); NativeClient.returnNative(returnVal);
} }
catch (e) { catch (e) {
@ -124,3 +119,9 @@ function __invokeMethod(objectName, functionName, stringifiedArgs) {
NativeClient.returnNative(""); NativeClient.returnNative("");
} }
} }
function _prepared() {
window.setTimeout = Reflect.get(window, "doricSetTimeout");
window.setInterval = Reflect.get(window, "doricSetInterval");
window.clearTimeout = Reflect.get(window, "doricClearTimeout");
window.clearInterval = Reflect.get(window, "doricClearInterval");
}